Question by
Lukas-Barbarossa · Mar 20, 2020 at 12:31 PM ·
scripting problemexecution order
Ensuring script execution order with Awake()
Hi folks.
Following advice after Unity's script execution order didn't guarentee the order in which scripts are loaded, I created a class to load my 5 managers in Awake(). The idea is, that coupled scripts can then use Start() to get access to each other using GameObject.Find("ObjectName").GetComponent
public class InstantiateManagers : MonoBehaviour
{
public GameObject board; //the Board prefab
public GameObject pieceManager; //the PieceManager prefab
public GameObject matchManager; //the MatchManager prefab
public GameObject lineManager; //the LineManager prefab
public GameObject virusManager; //the VirusManager prefab;
// Done in Awake() so that objects with coupled scripts exist when assigned in Start()
private void Awake()
{
//Instantiate the managers in the order required
GameObject latestManager = Instantiate(board, this.transform.position, this.transform.rotation);
latestManager.name = "Board";
latestManager = Instantiate(pieceManager, this.transform.position, this.transform.rotation);
latestManager.name = "PieceManager";
latestManager = Instantiate(matchManager, this.transform.position, this.transform.rotation);
latestManager.name = "MatchManager";
latestManager = Instantiate(lineManager, this.transform.position, this.transform.rotation);
latestManager.name = "LineManager";
latestManager = Instantiate(virusManager, this.transform.position, this.transform.rotation);
latestManager.name = "VirusManager";
}
}
I was sure this would work, as Awake() executes before Start(), but despite this code instantiating the objects perfectly well, when VirusManager tries to access the Board class it is null.
The GameObject.Find GetComponent lines are absolutely correct in VirusManager.Start(). Can anyone suggest something I might be doing wrong, or an alternative approach?
Comment
Your answer
