如何继续检查游戏对象是否将在统一层次结构中实例化?

How to keep checking if a gameobject will be instantiated in hierarchy in unity?

我想继续检查游戏对象是否 instantiated.All 我有游戏对象名称说 'Model'。如果模型在层次结构中处于活动状态,我想做 something.The 问题最初是游戏对象不是 available.Later 只有 instantiated.How 检查具有名称的游戏对象是否存在于将成为克隆的层次结构中。

if(gameobject.name=="Model(Clone)") { //Do something }

这将 return 一个 空值 因为它没有被实例化 initially.After 一段时间后它会被实例化。

如果是我我会这样做

List<GameObject> models = new List<GameObject>;
public GameObject baseModel; //Your model

private void CreateModel()
{
   GameObject obj = GameObject.Instantiate(baseModel) as GameObject;
   models.Add(obj);
}

这样你就可以使用它了。

void Update()
{
   if(models.Count > 0)
   {
       //Do Something
   }
}

请记住,如果您要销毁该对象,请不要忘记这样做。

private void DestroyModel(GameObject obj)
{
   models.Remove(obj);
   Destroy(obj);
}

这是将游戏对象分配到某处的好习惯。哈哈

如果您的代码中有一个属性 "gameobject" 导致您提供的代码抛出空异常,只需执行

if(gameobject!=null && gameobject.name=="Model(Clone)")
{
    //Do something
}

相反。

如果您无权访问该游戏对象而只想检测它是否存在,请尝试类似

var go = GameObject.Find("Model(Clone)");
if(go!=null)
{
    //Do something
}

或者如果您的游戏对象有一些脚本组件 MyScript,您可以这样做

var gos = GameObject.FindObjectsOfType<MyScript>();
if(gos!=null && gos.Length > 0)
{
    // foreach(var go in gos) {}
    //Do something
}

  • 初始化变量模型
    Gameobject Model;

  • 当模型尚未实例化时,逻辑断言将returned false

    if (Model != null && Model.name == "Model(Clone)"){//return false
        //Do Something
    }
    

  • 用名称实例化游戏对象。
    Gameobject Model = Instantiate (somePrefab);

  • if 需要放在 void update() 里面,或者像 invoke() 这样的循环,它会不断检查条件。

  • 当 Gameobject Model 不为 null 并且存在名为 Model(Clone)

    的游戏对象时,它将检查 if 和 return true
    if (Model != null && Model.name == "Model(Clone)"){//return true
        //Do Something
    }
    
  • 最好的选择是使用 UnityEvents。
    基本上,当您想要 运行 一段代码时,只需将其注册为 UnityEvent 的侦听器即可。所以当你做某事时,说 Instantiate 一个对象,你可以 Invoke 紧随其后的那个 UnityEvent,它会 运行 你注册到它的所有侦听器,无论那些片段在哪里代码在你的项目中。

    您首先需要编写 Instantiates 游戏对象和 Invokes UnityEvent 的代码(这整个事情都在一个 .cs 文件中,顺便说一句):

    public class GameObjectSpawner : MonoBehaviour 
    {
        public GameObject GameObjectPrefab;
        public GameObjectEvent OnCreateObject;
    
        private void Update() 
        {
            // Everytime you click your left mouse button
            // Only runs once per mouse click
            if (Input.GetMouseButtonDown(0)) 
            {
                // Instantiate the gameobject...
                GameObject go = Instantiate(GameObjectPrefab);
    
                // Then immediately Invoke our event to run all the listeners
                // and passing our gameobject to them.
                OnCreateObject.Invoke(go);
            }
        }
    }
    
    // This is just so the code that 'listens' to the event can get access to the 
    // gameobject that just got instantiated.
    public class GameObjectEvent : UnityEvent<GameObject> { }
    

    因此,如果您的项目中的其他地方有代码需要 运行 当该游戏对象为 Instantiated 时,该代码 运行 仅在您的游戏对象被实例化时,永远不会之前(除非您手动 运行 该代码)。将该代码注册为 OnCreateObject 事件的侦听器就这么简单(这可以在不同的 .cs 文件中):

    public GameObjectSpawner Spawner;
    
    private void Start()
    {
        // You can place this anywhere you want to, but we're
        // placing this here in Start just to make sure that
        // we're already listening waaay before the gameobject
        // is instantiated.
        Spawner.AddListener(CheckHierarchy)
    }
    
    // It doesn't matter if this is public or private,
    // as long as it's void and takes in a GameObject as a parameter.
    private void CheckHierarchy(GameObject go)
    {
        // Now we can do all kinds of things to the newly instantiated
        // gameobject, like check if it's active in the hierarchy!
        if (go.activeInHierarchy == true)
        {
            Debug.Log("I LIVE!!!");
        }
    }
    

    UnityEvents 的功能远不止于此,但这只是帮助您开始使用它的要点。祝你好运!