You can change Script Execution Order.
You can use the Script Execution Order settings (menu: Edit > Project Settings, then select the Script Execution Order__ category).
You could also add a safecheck in your code:
public void LoadFile()
{
if(!HasBeenInitialized) Start();
if (_sPath == null)
{
Debug.Break();
}
//do something with _sPath
}
EDIT ----- ANSWER TO PREVIOUS QUESTION BELOW
First of all check that your Unity version is not an Alpha one. In that case anything might happen.
If your unity version is anyone of 2019.1 or previous everything will be working as expected.
Awake is called after all objects are initialized
However, Awake is called when the script object is initialised, regardless of whether or not the script is enabled. Start may not be called on the same frame as Awake if the script is not enabled at initialisation time.
This means your Awake and Start are called properly (because your game object starts enabled). You may just add a Debug.Log statement to check them.
public void Awake()
{
Debug.Log("Awake Called");
_sPath = Application.persistentDataPath + "/somefilename.dat";
}
public void Start()
{
Debug.Log("Start Called");
_sPath = Application.persistentDataPath + "/somefilename.dat";
}
public void LoadFile()
{
if (_sPath == null)
{
Debug.Break();
}
//do something with _sPath
Debug.Log(_sPath);
}
The issue relies most likely on MainScript method.
Also:
- Make sure the scripts are saved before launching play
- Make sure the scripts are attached to the Game Objects before going in play mode