首页 > 解决方案 > 为什么我的 powerup 预制件可以工作,但实例化的克隆却不行?

问题描述

这是我的播放器脚本中的代码:

  void OnTriggerEnter2D (Collider2D collider2D)
     {
         if (collider2D.gameObject.tag == "powerup")
         {
             PoweredUp = true;
             PowerupTimer = PowerupTimer;
             PowerupTimer -= Time.deltaTime;
         }
     }

...这是上电本身:

 void OnTriggerEnter2D(Collider2D collider2D)
 {
     if (collider2D.gameObject.tag == "Player")
     {
         Destroy(gameObject);
     }
 }

出于某种原因,我直接放入场景中的任何预制件都可以正常工作,但是实例化的预制件会被破坏,但由于某种原因,不要触发“PoweredUp”布尔值。

这是我在 spawner 对象上的一个更复杂的脚本:

  public GameObject powerup;
     public float minWait;
     public float maxWait;
 
     private bool isSpawning;
 
     IEnumerator SpawnObject(float seconds)
     {
         Debug.Log("Waiting for " + seconds + " seconds");
 
         yield return new WaitForSeconds(seconds);
         Instantiate(powerup, transform.position, transform.rotation);    
         isSpawning = false;
     }
 
     void Update()
     {
         if (!isSpawning)
         {
             isSpawning = true;
             StartCoroutine(SpawnObject(UnityEngine.Random.Range(minWait, maxWait)));
         }

有什么解决方案,或者我可以更有效地做到这一点的方法吗?

更新:我发现 Player 对象上的脚本运行良好,但布尔值仍然没有改变。

标签: unity3d

解决方案


好的,我明白了,有一个编译器警告引用了“PowerupTimer = PowerupTimer”这一行,所以我将其更改为“PowerupTimer = 7f”,这几乎是一回事。


推荐阅读