为什么我的 switch 的大小写是正确的,但输出却不正确?

Why would my switch's case be correct but the output is not?

我的播放器有一个功能,可以在短时间内突出显示敌人,这一切都很有效。我决定让这个升级,这样能力的等级越高,突出显示的东西就越多。

为此,我认为 IEnumerator(协程)中的开关 运行 是最有效的方法。一个函数调用协程,并且根据玩家的能力水平,开关应该处理实际的能力功能(见代码)。

代码如下:

public void CatsEye(){
  if (player.cateye >= 1 && !catsEyeActive) {

    player.cateye -= 1;
    StartCoroutine("CatEye");
      // Debug.Log("Cateye Active");

  }
}


IEnumerator CatEye(){
  catsEyeActive = true;

    switch(player.cateyeLevel){
      case 3:
      if (itemObject != null && enemyObject != null
          && specialObject != null && hiddenObject != null) {
        hidden.isOutlined = true;
        special.isOutlined = true;
        enemyData.isOutlined = true;
        item.isOutlined = true;
      }
      yield return new WaitForSeconds(15f);
      if (itemObject != null && enemyObject != null) {
        hidden.isOutlined = false;
        special.isOutlined = false;
        enemyData.isOutlined = false;
        item.isOutlined = false;
      }
      catsEyeActive = false;
      break;


      case 2:
        if (itemObject != null && enemyObject != null && specialObject != null) {
          special.isOutlined = true;
          enemyData.isOutlined = true;
          item.isOutlined = true;
        }
        yield return new WaitForSeconds(15f);
        if (itemObject != null && enemyObject != null) {
          special.isOutlined = false;
          enemyData.isOutlined = false;
          item.isOutlined = false;
        }
        catsEyeActive = false;
      break;


      case 1:
        if (itemObject != null && enemyObject != null) {
          enemyData.isOutlined = true;
          item.isOutlined = true;
        }
        yield return new WaitForSeconds(15f);
        if (itemObject != null && enemyObject != null) {
          enemyData.isOutlined = false;
          item.isOutlined = false;
        }
        catsEyeActive = false;
      break;


      case 0:
        if (enemyObject != null) {
          enemyData.isOutlined = true;
        }
        yield return new WaitForSeconds(15f);
        if (enemyObject != null) {
          enemyData.isOutlined = false;
        }
        catsEyeActive = false;
      break;
    }
}

问题是,即使 player.cateyeLevel 设置为 0,该能力的表现就好像它处于 3 级并且突出显示所有内容。

player.cateyeLevel 指的是播放器脚本,但那里没有设置可能导致问题的变量。事实上,如果有的话,这个能力的 player.cateyeLevel 变量已经设置为 0。也没有 PlayerPrefs 存储 player.cateyeLevel,所以也不是。

有人知道吗?

通过完全重写功能解决了这个问题,任何有兴趣的人的新代码都使用 Update 作为主要的大纲,协程现在只用于计时。

    void Update()
    {

      enemyObjects = GameObject.FindGameObjectsWithTag("Enemy");
      foreach(GameObject enemyObject in enemyObjects){
        enemyData = enemyObject.GetComponent<Enemy>();
        if (enemyObject != null) {
          if (catsEyeActive && player.cateyeLevel == 0) {
            enemyData.isOutlined = true;
          }
        }
      }

      items = GameObject.FindGameObjectsWithTag("Item");
      foreach(GameObject itemObject in items){
        item = itemObject.GetComponent<Item>();
        if (item != null) {
          if (catsEyeActive && player.cateyeLevel == 1) {
            enemyData.isOutlined = true;
            item.isOutlined = true;
          }
        }
      }

      specials = GameObject.FindGameObjectsWithTag("Special");
      foreach(GameObject specialObject in specials){
        special = specialObject.GetComponent<Special>();
        if (special != null) {
          if (catsEyeActive && player.cateyeLevel == 2) {
            enemyData.isOutlined = true;
            item.isOutlined = true;
            special.isOutlined = true;
          }
        }
      }

      hiddens = GameObject.FindGameObjectsWithTag("Hidden");
      foreach(GameObject hiddenObject in hiddens){
        hidden = hiddenObject.GetComponent<Hidden>();
        if (hidden != null) {
          if (catsEyeActive && player.cateyeLevel == 3 ) {
            enemyData.isOutlined = true;
            item.isOutlined = true;
            special.isOutlined = true;
            hidden.isOutlined = true;
          }
        }
      }

      
    }


    public void CatsEye(){
      if (player.cateye >= 1 && !catsEyeActive) {    
        player.cateye -= 1;
        StartCoroutine("CatEye");
      }
    }


    IEnumerator CatEye(){
      catsEyeActive = true;
      yield return new WaitForSeconds(15f);
      catsEyeActive = false;
    }