- Home /
change values after all update function of multiple objects of same script is called
So, I have been instantiating a gameobject with a script attached, say S, to it. Each gameObject I instantiate is having different name. The Script S attached to each of this gameObject has three variables row, column & name. I know that the order in which these objects re instantiated will have their Update function executed first. Scripts S's Update function makes some modifications to these variables but since the execution is not simultaneous & an update of one affects the update of other.
For example : I have instantiated a gameobject A with script values : row=2, column = 1, name = A & then instantiate another gameobject B with row=2, column=3, name = B. Update function of A changes row to 1 & column to 3 & Update of B changes row to 3 column to 0. What i want is to send the values of row & column to another script. But, since B gets executed after A, values sent by A gets overwritten by values sent by B.
I want to know if their is a way by which I can write a function after all the updates of same script(but different objects) have been executed?
Thanks in Advance!!
I'm not getting what you want the execution order to be finally.
Could you just pass the variables to the "another script" (X) by calling a method in it and on top of taking in the values, that method would do all the necessary actions with those variables. Since each "S" calls that function, X will perform the necessary actions with all the values passed from all instances of S.
If X does something where it needs the info from all instances of S simultaneously to make the necessary actions, then you'll just have to make X collect a list of parameters and keep track of how many Ss have run their Update and do what needs to be done after they all have Updated
Sorry for the ambiguity in question. Let me keep it simple this way. How to run a function after update function of all the objects with same script is completed?
Answer by Kiwasi · Nov 27, 2014 at 10:53 PM
Try LateUpdate. LateUpdate is designed for things like camera movement that must happen after all other scene movement.
Note that relying on scripts to execute in the order they were instantiated is asking for trouble. There is no guarantee this will work on any particular build platform, or that this behaviour won't change in the future.
LateUpdate is called just after Update. So if you have multiple objects with same script, it will call update & late update of one script & then move to next script for doing the same. What I want is, to write a function that I can call once update function of all the scripts is complete.
In that case you are better off with a $$anonymous$$anager. $$anonymous$$aintain all of the scripts in a List. Call your own update method on each of them from your manager. Then call your function.
This method also gives you a firm guarantee on the order of calling.
Your answer
Follow this Question
Related Questions
different objects with the same script wouldn't react correctly 1 Answer
Calling a function from same script, on different objects, through Inspector. 2 Answers
Instantiate Different GameObject prefabs? 1 Answer
3D texture went wrong 1 Answer
Is bug in unity?position error at some place on screen 0 Answers