Unity中的定时器错误

时间:2014-10-17 23:20:08

标签: c# timer unity3d

大家好我遇到了错误。一旦你触发了触发器,我试图制作一个计时器来产生一个敌人。计时器倒计时,但我遇到了这个错误

控制不能从一个案例标签“案例”EnemyTrigger“”转移到另一个案例。

以下是代码:

 using UnityEngine;
 using System.Collections;

 public class SpawnManager : MonoBehaviour {

 public GameObject Enemy;
 public float mytimer = 5.0f;

 void OnTriggerEnter(Collider other)
 {
     switch(other.gameObject.name)
     {
         case "EnemyTrigger":

             if(mytimer > 0){
                 mytimer -=Time.deltaTime;
                 }
             else 
             if(mytimer<=0){

                 Destroy(other.gameObject);

                 Transform enemy;
                 GameObject enemySpawnPoint = GameObject.Find("EnemySpawn");

                 enemy =      Instantiate(Enemy,enemySpawnPoint.transform.position,enemySpawnPoint.transform.rotation) as Transform; 
                 Debug.Log("Spawned Enemy");
                 break;
                 }
         }
     }
}

有人可以向我解释错误是什么吗?而且我该怎么做才能改变它?

1 个答案:

答案 0 :(得分:0)

解决方案

switch(other.gameObject.name)
{
    case "EnemyTrigger":

        if(mytimer > 0)
            mytimer -=Time.deltaTime;

        else if(mytimer<=0)
        {   
            Destroy(other.gameObject);

            Transform enemy;
            GameObject enemySpawnPoint = GameObject.Find("EnemySpawn");

            enemy = Instantiate(Enemy,enemySpawnPoint.transform.position,
                        enemySpawnPoint.transform.rotation) as Transform; 
            Debug.Log("Spawned Enemy");

        }

    break;
}

您收到错误是因为break语句中隐藏了case的相应else-if