- Home /
How to get in sync two scripts if the Update () calls are random?
What am aiming to do is simple: I want to clamp the object´s localScale when something happens in other script (it needs to be other script). However, that info may arrive late.
void Update () {
float scaleX = (currentTotalX - GetSumGrounds(groundBases) - cornerX) / sumPercentageGround * percentageGround / filter.mesh.bounds.size.x;
if (chain == true)//this is the info. This needs to be right before executing the rest of the code
{
if (doOnce == false)
{
clampValue = scaleX; //this supposing is on time
doOnce = true;
}
}
else
{
clampValue = 0;
}
scaleX = Mathf.Clamp(scaleX, clampValue, float.MaxValue);
transform.localScale = new Vector3(scaleX, transform.localScale.y, transform.localScale.z);
}
void Chain()
{
chain = true;
if (nextObject.tag != "endOfChain")//this can be a layer or anything else
{
nextObject.GetComponent<thisScriptName>().Chain();
}
}
The other script that I have starts the chain when 4 bools are true. If that happens, the last object in the chain changes its tag to "endOfChain" ant the void Chain in the first one starts.
But I think that if those functions are called late, the chain bool will be false when the code checks for it and clampValue won´t be clamped until the next frame. Therefore, the scale will be smaller than it should.
Can't you just call the update yourself? If you want to depend on something happening in order, you should generally force it yourself.
Also you can just use script execution order, but it probably is an overkill in this situation.
How do you say I can call the Updates without using the script execution order? Why is it an overkill? It would be useful to make a specific script act after the other ones, but still run before LateUpdate(). Does the script execution order work this way? Is the LateUpdate() of every other object still run after that?
In Script Execution Order you set the order in which Unity will cal methods like Start, Awake or Update. So you can simply arrange your scripts in the order you want them to be called. It is usually not recommended to do so, as it makes expanding your code harder, especially when you come back to it after some time or when somebody else starts working with your project. For this reasons (and probably much more) using script execution order is consider a last resort solution.
What you should try to do it call the "Updates" yourself. Let's say you have classes A, B and C and you need them to be called in order. I will suggest two solutions which are actually quite similar.
You can use Unity Update in class A and at the very end call a method in class B which at the very end calls a method in class C. Code:
class A { void Update() { //logic B.CustomUpdate(); } }
class B { void CustomUpdate() { //logic C.CustomUpdate(); } }
class C { void CustomUpdate() { //logic } }
This way it's clearly visible what's happening and that those methods should be in order.
You can create a manager that calls all three updates one after another.
class Update$$anonymous$$anager { void Update() { A.CustomUpdate(); B.CustomUpdate(); C.CustomUpdate(); } }
Answer by CarpeFunSoftware · Dec 05, 2018 at 02:53 PM
How about "Don't use Update()"??? Call it when you want it to happen. Or create an event delegate of some sort and invoke as needed. Maybe change the class structure/design.
You don't have to wait around for Unity to call your routines if you need a certain order.
Another way: Create a controller that has update(), and works with an "array" of registered objects, and works out along your "chain"...whatever that is. ("Linked list of objects or object dependencies"?) Etc.
There's 50 ways, but don't get "hung up" on Update() if that function's order of processing is in your way. Think about the design/architecture of whatever it is that you're doing. Make sure it's not a design problem.
I have code that does my own updates using event delegates: MangedUpdate(), OddFrameUpdate(), EvenFrameUpdate() that the game controller calls. Just as an example. The engine provides these nice functions, and you should work with them when you can, but you may need to call things yourself if you need different functionality. But if I need one thing updated after another, I'dd have the "other" call the thing that needs later-updates.
Answer by FoodLover195 · Dec 03, 2018 at 04:11 AM
Run it in the LateUpdate() function. https://docs.unity3d.com/ScriptReference/MonoBehaviour.LateUpdate.html
Actually, I can´t do that. You see, am already using LateUpdate() for the objects´positions, which depend on their scales (precisely the reason it´s done in LateUpdate()). Also, it may look weird.
Please give more details about your other script, and all the relevant pieces of code. Also an overview of what you're trying to do. As far as I know you can't guarentee one update before another unless one of those updates was in the late update function. I can only try and find another solution.
Your answer
![](https://koobas.hobune.stream/wayback/20220612183220im_/https://answers.unity.com/themes/thub/images/avi.jpg)