如何在销毁前一个对象的同一位置实例化游戏对象 [英] How to Instantiate a game object at the same position where the previous is destroyed

查看:203
本文介绍了如何在销毁前一个对象的同一位置实例化游戏对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个空游戏对象作为生成游戏对象的生成点,并且我希望破坏生成的游戏对象,如果要测试的条件为真,则希望在相同的生成点上实例化一个新对象.

I have multiple empty game objects that serve as the spawn points that spawn the game objects and I want the spawned game objects to be destroyed and Instantiate a new one on same spawn point if the condition to be tested is true.

我有2个单独的脚本,一个附加在生成点对象上,另一个用于其中具有条件的游戏管理员.

I have 2 separate scripts, the one attached on the spawn point objects and another for the game manager that has the condition in it.

游戏管理器脚本上的条件:

public void checkword()
 {
     wordBuilded = displayer.text.ToString();

     LetterTiles[] tiles = FindObjectsOfType<LetterTiles>();

     foreach (LetterTiles item in tiles)
     {

         if (txtContents.Contains(wordBuilded))
         {
             if (item.gameObject.CompareTag("clicked"))
             {
                 Destroy(item.gameObject);
                 FindObjectOfType<letterSpawner>().refresh();
             }                                                     
         }

         else
         {
             if (item.gameObject.CompareTag("clicked"))
                 item.GetComponent<Button>().interactable = true;
         }

     }
 }

附加到生成点对象的脚本,用于实例化对象

using UnityEngine;

public class letterSpawner : MonoBehaviour {

     public GameObject[] letterTiles;
     GameObject tiles;
     Vector3 scale = new Vector3(0.8f, 0.8f, 0);

     void Start () {
         refresh();
     }

     public void refresh()
     {
         int rand = Random.Range(0, letterTiles.Length);
         tiles = Instantiate(letterTiles[rand], transform.position, Quaternion.identity);
         tiles.transform.SetParent(gameObject.transform);
         tiles.transform.localScale = scale;

     }
 }

推荐答案

您可以通过对现有内容进行一些小的更改,首先将刷新功能更改为该功能来实现这一目的

You can do that by making a small change to whatever you have, first change your refresh function into this one

public void refresh(Vector3 position)
 {
     int rand = Random.Range(0, letterTiles.Length);
     tiles = Instantiate(letterTiles[rand], position, Quaternion.identity);
     tiles.transform.SetParent(gameObject.transform);
     tiles.transform.localScale = scale;

 }

还在同一文件中添加另一个默认文件,该文件使用您拥有的默认值调用该文件

also in the same file add another default one that calls this with the default value that you had

public void refresh()
 {
     refresh(transform.position);
 }

,然后在您的checkword函数中

if (item.gameObject.CompareTag("clicked"))
{
    Vector3 pos = item.transform.position;
    Destroy(item.gameObject);
    FindObjectOfType<letterSpawner>().refresh(pos);
}

应该为您做

这篇关于如何在销毁前一个对象的同一位置实例化游戏对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆