Unity 2D重生实例化问题

时间:2014-11-28 22:12:01

标签: unity3d unityscript unity3d-2dtools

我正在制作2D太空入侵者风格的游戏,并且在重生时遇到了很大的困难。我想要发生的是当太空战被击中时,它被毁灭,e.x。破坏();

但是当我尝试实例化()SpaceShip的预制时它会解决(克隆)(克隆)问题,因此我尝试制作一个空的GameObject运行脚本,该脚本在SpaceShip的预制件中生成,当它发生时得到Destroy();它在3秒后重生。非常感谢,我用JavaScript编写代码。

1 个答案:

答案 0 :(得分:0)

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

public class Spawner : Monobehaviour
{
    public GameObject playerPrefab;

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

然后,当您Player去世时,您可以SendMessageSpawner课程。

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

中的相同代码

Spawner.js

  var playerPrefab : GameObject;

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

Player.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);
      }
  }