- Home /
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
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?
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.
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.
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/
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.
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?
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.