C# 当布尔值为true时,如何调用IEnumerator?

C# 当布尔值为true时,如何调用IEnumerator?,c#,function,unity3d,ienumerator,C#,Function,Unity3d,Ienumerator,我正在使用IEnumerator函数,并且在if语句工作时遇到了一些问题: IEnumerator Spawning() { Debug.Log("Spawning being called..."); if (GameObject.Find("FPSController").GetComponent<BoxCollide>().hitTrigger == true) { Debug.Log("CUSTO

我正在使用IEnumerator函数,并且在if语句工作时遇到了一些问题:

IEnumerator Spawning()
    {
        Debug.Log("Spawning being called...");
        if (GameObject.Find("FPSController").GetComponent<BoxCollide>().hitTrigger == true)
        {
            Debug.Log("CUSTOMER SHOULD SPAWN!");
            while (countStop == false) {

                yield return new WaitForSeconds(2);
                Debug.Log("Fisher Spawned!");
                counter++;
                spawnNewCharacter.SpawnCharacter();

                if (counter >= 3){
                    countStop = true;
                }

            }
        }
    }
但仍然无法通过任何
Debug.Log
。如果您能在这方面提供帮助,我将不胜感激

旁白 Find()和GetComponent()

您不想在Update方法中使用GameObject.Find(…),因为这是一个昂贵的调用。Update方法在每帧调用一次,因此您可以在1秒内以60fps的速度调用GameObject.Find(…)60次。 因此,当您使用GetComponent()或Find()时,您希望保存对这些对象的引用,如下面的代码段所示

使用GetComponent()或GameObject.Find()等方法的更好位置是和方法

清醒

唤醒用于在启动之前初始化任何变量或游戏状态 比赛开始了。在对象的生命周期中,唤醒只调用一次 脚本实例。在初始化所有对象之后调用Awake,以便 您可以安全地与其他对象对话或使用例如查询它们。 GameObject.FindWithTag。[...] 解释摘自链接文档。

开始

在任何脚本之前启用脚本时,在帧上调用Start 第一次调用更新方法中的一个。就像清醒的人一样 函数,则在脚本的生命周期中只调用一次Start。 但是,初始化脚本对象时会调用Awake, 无论脚本是否已启用。开始可能不是 如果此时未启用脚本,则在与唤醒相同的帧上调用 初始化时间。 解释也取自链接的文档


可能的解决办法 将第一个组件(FPSControllerCollection)添加到容纳FPSController的对象上。 它运用了统一性和方法

当触发器进入框碰撞器的空间时,此脚本将IsTriggered bool设置为true

注意:当“是触发器”复选框处于启用状态时,碰撞器充当触发器 该组件已被检查

当碰撞器进入长方体碰撞器的空间时,可以使用/Exit执行类似的操作来识别

请注意,以下只是一个示例,您必须进行调整/ 将其集成到代码中

以下SpawnController类可以集成到allready拥有的类中

  public class SpawnController : MonoBehaviour {
    private FPSControllerCollission _fpsControllerCollission;

    private void Awake() {
      this._fpsControllerCollission = FindObjectOfType<FPSControllerCollission>();
    }

    private void Update() {
      if (this._fpsControllerCollission.IsTriggered) {
        StartCoroutine(nameof(Spawning));
      }
    }

    IEnumerator Spawning() {
      Debug.Log("Spawning being called...");
      if (this._fpsControllerCollission == true) {
        Debug.Log("CUSTOMER SHOULD SPAWN!");
        bool countStop = false;
        int counter;
        while (countStop == false) {

          yield return new WaitForSeconds(2);
          Debug.Log("Fisher Spawned!");

          counter++;
          spawnNewCharacter.SpawnCharacter();

          if (counter >= 3) {
            countStop = true;
          }

        }
      }
    }
  }
公共类控制器:单行为{
私人FPSControllerCollection\u FPSControllerCollection;
私人空间{
此._fpscollercission=FindObjectOfType();
}
私有void更新(){
如果(此.\u FPSControllerCollection.IsTriggered){
Start例程(名称(产卵));
}
}
IEnumerator产卵(){
Log(“正在调用的生成…”);
if(this.\u fpscollercission==true){
Log(“客户应该生成!”);
bool countStop=false;
整数计数器;
while(countStop==false){
产生返回新WaitForSeconds(2);
Log(“Fisher繁殖!”);
计数器++;
spawnNewCharacter.SpawnCharacter();
如果(计数器>=3){
countStop=true;
}
}
}
}
}

这里没有问题要回答;这不是为您调试错误代码的服务。你有一个有答案的问题吗?我会注意到,与真或假进行比较肯定是初学者代码的标志。如果(x==true),千万不要说
;那看起来很业余。你问的是“x是真的吗?”而不是“x是真的吗?”只要说
if(x)
。不要在(x==false)
时说
。在(!x)
时说
。嗨@EricLippert,谢谢你的提示。我只是想弄清楚,当hitTrigger在我的另一个脚本中变为true时,我如何调用IEnumerator函数。@toadflax以你想要的方式编写你的if语句,如果说
if(x==true)
“看起来很业余”是没有建设性的,如果它有助于你阅读和理解你要做的事情,那么说
if(x==true)
,没有什么错,那么这就是最重要的了。@toadflax您可以包括您的其他脚本,以便我们可以看到您是如何将hitTrigger设置为true/false的吗?
  [RequireComponent(typeof(BoxCollider))]
  public class FPSControllerCollission : MonoBehaviour {
    public bool IsTriggered;

    private void OnTriggerEnter(Collider other) {
      this.IsTriggered = true;
    }

    private void OnTriggerExit(Collider other) {
      //Maybe check the tag/type of the other object here
      this.IsTriggered = false;
    }
  }
  public class SpawnController : MonoBehaviour {
    private FPSControllerCollission _fpsControllerCollission;

    private void Awake() {
      this._fpsControllerCollission = FindObjectOfType<FPSControllerCollission>();
    }

    private void Update() {
      if (this._fpsControllerCollission.IsTriggered) {
        StartCoroutine(nameof(Spawning));
      }
    }

    IEnumerator Spawning() {
      Debug.Log("Spawning being called...");
      if (this._fpsControllerCollission == true) {
        Debug.Log("CUSTOMER SHOULD SPAWN!");
        bool countStop = false;
        int counter;
        while (countStop == false) {

          yield return new WaitForSeconds(2);
          Debug.Log("Fisher Spawned!");

          counter++;
          spawnNewCharacter.SpawnCharacter();

          if (counter >= 3) {
            countStop = true;
          }

        }
      }
    }
  }