P
P
Pickto2017-04-08 19:09:27
C++ / C#
Pickto, 2017-04-08 19:09:27

Why does the code work out of order?

There is such code -

foreach (GameObject o in GameObject.FindGameObjectsWithTag("loc"))
            {
                Destroy(o);
            }
            Instantiate(GameObject.Find("Camera").GetComponent<LibLevels>().Levels[GameData.locID], new Vector3(), Quaternion.identity);
List<GameObject> Doors = new List<GameObject>(GameObject.FindGameObjectsWithTag("door"));
            for (int i = 0; i < Doors.Count - 1; i++)
            {
                GameObject g = Doors[i];
                if (GameData.isOpen[i])
                {
                    Debug.Log(g.name);
                    g.GetComponent<Door>().Open();
                }
            }

The scene initially has a gate object with a door tag, this object is a child of the "o" object in
the GameObject.FindGameObjectsWithTag("loc") list, so it must be destroyed before GameObject.FindGameObjectsWithTag("door")) occurs. Instantiate(GameObject.Find("Camera").GetComponent().Levels[GameData.locID], new Vector3(), Quaternion.identity) Creates a prefab in the scene where there is a gate1 with the "door" tag.
The problem is that the output of Debug.Log(g.name) produces gate , not gate1 as it should. Moreover, gate1 is not output later.
What is the reason?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2017-04-08
@Pickto

Destroy does not guarantee instant destruction and updating of the entire tree of objects, so Find can return it within the same Update.
Well, I really hope that this is not production code...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question