Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Feref2 · Dec 03, 2018 at 03:45 AM · variablesupdate functionsynchronizing

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Casiell · Dec 03, 2018 at 07:55 AM 1
Share

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.

avatar image Feref2 Casiell · Dec 03, 2018 at 10:32 PM 0
Share

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?

avatar image Casiell Feref2 · Dec 05, 2018 at 10:01 AM 1
Share

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.

  1. 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.

  1. 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(); } }

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by FoodLover195 · Dec 03, 2018 at 04:11 AM

Run it in the LateUpdate() function. https://docs.unity3d.com/ScriptReference/MonoBehaviour.LateUpdate.html

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Feref2 · Dec 03, 2018 at 04:24 AM 0
Share

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.

avatar image FoodLover195 Feref2 · Dec 03, 2018 at 08:24 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

100 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Function is not updating the variable 1 Answer

How to synchronize a variable on the client and the host? 1 Answer

How can I add to an existing variable only once? 2 Answers

Variable goes very high in update method 1 Answer

Error Accessing Other Script's Variables 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges