1

我正在制作一个 2D 太空入侵者风格的游戏,并且在重生时遇到了很大的困难。我想要发生的是当宇宙飞船被击中时,它被摧毁,前 Destroy();

但是当我尝试实例化()太空船的预制件时,它会出现(克隆)(克隆)问题,所以我试图制作一个空游戏对象运行脚本,该脚本会在太空船的预制件中产生,并且当它被销毁( ); 它会在 3 秒后重生。非常感谢您的帮助,我正在使用 JavaScript 进行编码。

4

1 回答 1

1

有一个类每隔几秒钟从预制件中实例化一次。您需要通过编辑器进行拖动,playerPrefab并且需要指定生成的位置。

public class Spawner : Monobehaviour
{
    public GameObject playerPrefab;

    public void spawn()
    {
        Instantiate(playerPrefab, spawnPosition, spawnRotation);
    }
}

然后,当您Player去世时,您可以SendMessage上课Spawner

public class Player : MonoBehaviour
{
    void Update()
    {
        if(hp <= 0)
        {
             // Grab the reference to the Spawner class.
             GameObject spawner = GameObject.Find("Spawner");

             // Send a Message to the Spawner class which calls the spawn() function.
             spawner.SendMessage("spawn");
             Destroy(gameObject);
        }
    }
}

相同的代码UnityScript

生成器.js

  var playerPrefab : GameObject;

  function spawn()
  {
      Instantiate(playerPrefab, spawnPosition, spawnRotation);
  }

播放器.js

  function Update()
  {
      if(hp <= 0)
      {
          // Grab the reference to the Spawner class.
          var spawner : GameObject = GameObject.Find("Spawner");

          // Send a Message to the Spawner class which calls the spawn() function.
          spawner.SendMessage("spawn");
          Destroy(gameObject);
      }
  }
于 2014-11-28T22:27:26.670 回答