public class LazyInit<T>
{
private T _value;
private bool _initialized = false;
private InitializerDelegate _initializer;
public T v
{
get
{
ForceInit();
return _value;
}
set
{
_initialized = true;
_value = value;
}
}
public delegate T InitializerDelegate();
public LazyInit(InitializerDelegate initializer)
{
_initializer = initializer;
}
public void ForceInit()
{
if (!_initialized)
{
_value = _initializer();
_initialized = true;
}
}
}
I’m wondering why a Start() method is being called before an Awake() method in my mobile game; as I understand it, the Awake() call order is undefined but all of them are indeed called before any Start() methods begin being called.
My setup is pretty simple; I’m just following along a CodeMonkey video trying to make a UI popup in my game.
There’s a Testing.cs script attached to my “Managers” empty GameObject (where my GameManager.cs script is also attached):
The Start() method in that Testing.cs script is being called before the Awake() method in my Popup script (this popup is a prefab called PlopDistance and the script is attached to it:
if two objects are created at the same time, specifically within the same frame, both their “Awakes” will fire the moment they are instantiated, then on the next frame their “Starts” fire.
if two objects are created at different times, say one now and the next two seconds later, the first one will undoubtedly fire both its Awake and Start long before the other object could. The “all awakes before all starts” rule only counts on a per frame basis. if an object wasn’t created on that frame yet, it has to do its awake/start timing on later frames.
thus if you testing script existed in the scene as its was loading, but your PlopDistance prefab was instantiated in at a later time, even by a single frame, then the Awake/Start call order across both scripts cannot be guaranteed.
That’s because your PlopDistance object is instantiated after a call to Test.Start method. It is not possible to call Awake for object what does not exists yet. This is what’s happening in your scene:
Ahhhh yeah that makes sense. Duh. Jeez. I are Unity nub
Now I just need to figure out the necessary Quaternion for the UI text that spawns. It looks like it’s rotated 90 degrees too far clockwise maybe. Thankfully my camera faces the same direction all the time; just need to have the Quaternion face the camera I think.
Man, just about every single little thing in this game is a whole thing to figure out and I’m constantly stuck. I guess it’s just a fact of learning something new. Thanks for your help y’all, I really appreciate it!