Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 alexxUID · Sep 03, 2020 at 10:45 AM · startawakerace

Strange Race condition for one prefab

Following setup:

Here the master Volume is set:

     public class GameController : MonoBehaviour
     {
         [Header("Audio")]
         [SerializeField] float soundFXMasterVolume = 0.5f;
 
         public float GetSoundFXMasterVolume()
         {
             return soundFXMasterVolume;
         }
     }

And here it is referenced and read:

     public class ClipManager : MonoBehaviour
     {
         [SerializeField] GameObject positionedSoundPrefab = default;
         [SerializeField] ClipDefinition[] clipDefinitions = default;
         AudioSource audioSource = default;
         GameController gameController = default;
         float soundFXMasterVolume = 1f;
 
         SpawnedObjectsController spawnedObjectsController = default;
         Transform instanceParent = default;
 
         private void Awake()
         {
             audioSource = GetComponent<AudioSource>();
             gameController = FindObjectOfType<GameController>();
             spawnedObjectsController = FindObjectOfType<SpawnedObjectsController>();
         }
 
         private void Start()
         {
             soundFXMasterVolume = gameController.GetSoundFXMasterVolume();
             instanceParent = spawnedObjectsController.sound;
             
         }
 }

But unfortunately for one of several prefab types that is not working. The soundFXMasterVolume for that one prefab is not correctly returned. It seems I always get the very first value I set for the serialized field. To fix it, I have to move the line

 soundFXMasterVolume = gameController.GetSoundFXMasterVolume();

from the Start() method to Awake() - which from my understanding is not the correct way to do it.

The prefab that generates the problem is calling the clip manager in its Start() method, which all the others don't.

What could it be that I am missing to see?

Thanks for the help

Comment
Add comment · Show 4
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 Namey5 · Sep 03, 2020 at 11:03 AM 1
Share

Awake is always called before start, but the instructions within these calls occur synchronously and the only real race conditions happen between different $$anonymous$$onoBehaviours (by default it's impossible to tell which script will have its $$anonymous$$onoBehaviour function called first). I see no problem with moving the sfx assignment to Awake - but which variable in particular is incorrect (the one in Game$$anonymous$$anager or the one in Clip$$anonymous$$anager) and where/when/how is this being accessed that is causing the problem?

avatar image alexxUID Namey5 · Sep 03, 2020 at 12:00 PM 0
Share

soundFX$$anonymous$$asterVolume in the Clip$$anonymous$$anager is the variable that is not filled correctly. The same problem occurs with another component as well, where a serialized variable (a transform it is there) is not set, so the default null is returned. As I edited in: the call to the clip$$anonymous$$anager (and there a Play() method) is done from another Start() method in another script. I guess therein lies the problem as you described. I have found something about lazy variable initialization that I am going to look into, since it seems to handle exactely the problem I have. Thanks for the guidance.

avatar image Namey5 alexxUID · Sep 03, 2020 at 12:37 PM 0
Share

Yeah properties and singletons are definitely a good way to go to prevent this kind of thing (global managers are especially good as singletons). So long as you can guarantee the variables are set by the time you use them, it doesn't really matter - another way if you needed the variable declared at a later time (say, if necessary data isn't available), then you can also defer the access of that variable to until it is ready through flags, null checks, callbacks, etc.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by alexxUID · Sep 04, 2020 at 12:39 PM

@Namey5 @Bunny83 Oh boy.. I found out why it is behaving like it is, but I am not really sure why this is the way the stuff gets executed: Execution order in the Program: In PortalFactory class a Portal is instantiated. In the Portal classes Start() the sound is played using its component ClipManager (and there a method called PlayAtPosition()).

Here comes the fun part how Unity executes the call:

  • Awake GameController (yes - I have only one for sure)

  • Awake Portal

  • Awake ClipManager

  • Start Portal

  • PlayAtPosition() in ClipManager

  • Start ClipManager

This perfectly explaines why this all only works when the lines are in awake in my first post and why not if they are in the Start() method. But I thought Unity does all Awake and all Start before running a method in the components. Wow. This somehow terrifies me.

Edit: I found this from 2008 - exactely my question and a short answer: https://forum.unity.com/threads/when-is-start-on-script-instantiated-game-objects-run.10642/

Comment
Add comment · Show 3 · 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 Namey5 · Sep 04, 2020 at 01:02 PM 0
Share

Like I mentioned in my comment, all instructions made within a function occur synchronously. You can think about it in this way - calling a function from Start() would be equivalent to just copying and pasting the code within that function directly into the place where the function call currently is. If you want to actually see the order in which calls are made, you can use a debugger (like Visual Studio) and put a break point at the start of one of the relevant functions (Start, Awake, etc). From there you can step through the code one line at a time to see how code is executed, and you can also view the call stack to see all the previous function calls along the path that lead to the current point in execution.

avatar image alexxUID Namey5 · Sep 04, 2020 at 01:10 PM 0
Share

I failed to grasp, that it really executes the code without running the components Start(), which I though was always done as the initialization - but that is only Awake(), right?

avatar image Namey5 alexxUID · Sep 04, 2020 at 01:30 PM 0
Share

Awake and Start are run in the same frame but in that specific order across the scene, i.e. first, all Awake functions will be called, then all Start functions will be called. This is just Unity specific behaviour and has nothing to do with the flow of scripting. All code within those functions will be executed in the order they are written and don't really have any relation other than the fact that anything called from Awake will run before anything called from Start. All of the events we are talking about occur within the same frame, just at different times. If you want to do something on initialisation, it would probably be better to use OnEnable, but even then I'm not sure if the Unity loop updates until the next frame post-instantiation.

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

136 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 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

Race conditions using Instantiate 1 Answer

Instantiated Prefab not calling Start() nor Awake() 1 Answer

does finction start or awake run when the object or script is enabled mid game 3 Answers

Start, Awake, Update. Any other ways to call functions from an empty GameObject? 3 Answers

Initializing variables with ExecuteInEditMode 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