Hello,
I have a bunch of C# scripts that I put in the ‘script execution order’ of my project. ‘Script number 1’ created an object, but BEFORE the ‘cycle’ of all my scripts completed, I had ‘Script number 2’ try and work with the object that ‘Script number 1’ created, but doesn’t recognize the object that got created.
I don’t want to have to make script 2 wait until the next ‘cycle’ (under an Update() function) before Unity recognizes the object created. How would I do this so that other scripts would know when an object is created and destroyed under the same ‘cycle’?
Thank you,
Michael S. Lowe
If I understand correctly, what you refer to as a cycle is called a frame.
When you create an object, Unity has to do some voodoo on it before it actually exists in your scene (such as calling it’s Awake and Start). So you may not be able to grab the object from the scene on the frame you create it.
On the other hand, you can pass a reference to that object you created to another script that needs it, since that script would exist.
As a good practice, you should rely on script execution order as little as you can.
Can you post some code that is giving you trouble? Most things you do won’t be so heavily dependent on execution order, which suggests to me that you may be relying on certain things that are troublesome.
My situation is that I have an empty object called ‘world’, which I would like to have a 3x3x3 ‘grid’ of instantiated objects called ‘room_-1_-1_-1’ to ‘room_1_1_1’ (creating a total of 27 rooms, where the 27 objects make up the 3x3x3 grid and are children of the ‘world’ object). Then, each empty ‘room’ has a ‘grid’ of instantiated items and doorways that lead to the next room, so the items and doorways would be in a grid under the room objects, and the room objects would be a ‘grid’ under the world object. What if I wanted to set up which script goes first, and if the wrong script goes first, then the game will not set up correctly? That is why I need to use script execution order.
You could just have your scripts talk to each other directly via conventional relationships. A world HAS rooms. Rooms HAVE doorways. Why aren’t they setup in that fashion?
The world should process the rooms. The rooms should process the doorways.