Is it possible to find out from script if Unity is currently calling Awake() or Start(). For example:
void Awake()
{
bool loaded = game.isSceneLoaded("awesome_level");
}
**game class**
bool isSceneLoaded(string name)
{
if(UnityEngine.madeup_State == enumState_Awake)
{
Debug.LogError("this function must be called from Start()");
}
else
{
*check for loaded scene but only if we're called from Start()*
}
}
Not “automatically”. You have to parameterize and pass around that info just like any other piece of information in your code.
void Awake()
{
SomeFunction("scene", false);
}
void Start()
{
SomeFunction("scene", true);
}
bool SomeFunction(string name, bool calledFromStart)
{
if (calledFromStart) {
} else {
}
}
After thinking about it some more I thought about StackTrace. I know this may be overkill for something that might seem trivial but it could be helpful to others.
public bool isCalledFromAwake()
{
System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(false);
foreach(System.Diagnostics.StackFrame f in stackTrace.GetFrames())
{
if(f.GetMethod().Name.Equals("Awake")) return true;
}
return false;
}