首页 文章

Unity 2D重生实例化问题

提问于
浏览
0

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

但是当我尝试Instantiate()太空船的预制时,它会解决(克隆)(克隆)问题,所以我试图制作一个空的GameObject运行脚本,该脚本在SpaceShip的预制中生成,当它被Destroy时( );它在3秒后重生 . 非常感谢,我用JavaScript编写代码 .

1 回答

  • 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);
          }
      }
    

相关问题