Why the code does not work right?

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();
}
 }</door></gameobject></gameobject></liblevels>

Initially on the scene, the object gate with the door tag, this object docere object "o" in
the list of GameObject.FindGameObjectsWithTag("loc"), so it must be destroyed before going on a GameObject.FindGameObjectsWithTag("door")). Instantiate(GameObject.Find("Camera").GetComponent().Levels[GameData.locID], new Vector3(), Quaternion.identity) Creates on stage where gate1 is prefab with the tag "door".
The problem is that on the Debug output.Log(g.name) throws gate ,gate1 and not as it should be.What gate1 be output later.
What is the reason?
June 27th 19 at 15:07
1 answer
June 27th 19 at 15:09
Destroy does not guarantee instant destruction and renewal of the entire tree of objects, so Find may return it within the same Update.

And I very much hope that this is not production code...
No, of course. This is rough code.Any idea how to solve this problem? - grayce.Yun commented on June 27th 19 at 15:12
If anything, it is static function and there is no way to push back coroutine, at least I think so - grayce.Yun commented on June 27th 19 at 15:15
: The simplest objects are not destroyed, and shut down. Continue to look for niewykluczone objects (like there is in unity out of the box Find this). And then destroy the objects.
The most correct - to change the architecture to generally get rid of Find. - nicklaus commented on June 27th 19 at 15:18
: There find no pressure on performance, I see no reason to remove it, but shutting down is a good idea, will try, thanks. - grayce.Yun commented on June 27th 19 at 15:21
Now I know that this will not solve the problem because not immediately Instantiate the objects it creates, and then the output of find with the door tag will be null
going to change architecture - grayce.Yun commented on June 27th 19 at 15:24
: I'm about the find was not written in the context of performance. And in the context of connected objects. What you came after awareness of the troubles with the Instantiate. I would like Door.Open was moved to the Awake object. - nicklaus commented on June 27th 19 at 15:27
Chet startled me, but the problem was solved, there was a time when it wanted all the fuck you want to quit and go to sleep. - grayce.Yun commented on June 27th 19 at 15:30
: pfft, the ability to clubbing under the weight of bad code here )))) - nicklaus commented on June 27th 19 at 15:33

Find more questions by tags Unity Game EngineC#