销毁对象中的Unity C# StartCoroutine()

Ciu*_*sti 3 c# unity-game-engine

我在带有 C# 的 Unity 中有这个脚本,协程的时间不工作,第二行forwardForce = 10f没有被调用:

using UnityEngine;
using System.Collections;

public class SlowPickup : MonoBehaviour
{
  public float waitTime = 2f;

  private IEnumerator DecreaseSpeed ()
  {
     GameObject.Find("Player").GetComponent<PlayerMovement>().forwardForce = 1f;
     yield return new WaitForSeconds(waitTime);
     GameObject.Find("Player").GetComponent<PlayerMovement>().forwardForce = 10f;
  }

  void OnTriggerEnter ()
  {
    Destroy(gameObject);
    StartCoroutine(DecreaseSpeed());
  }

}
Run Code Online (Sandbox Code Playgroud)

han*_*daa 5

您正在销毁托管协程的 GameObject,这将销毁协程,导致其无法完成。您将希望在协程结束时销毁游戏对象,或使用静态协程管理器。