In my game on Unity3d I have 2 classes. One of them ActsGeneral
initialize variable gameManagerScript
and another Act_0
try to access it.
Class GameManager
is stored in object, which exists only on the first scene. I use function DontDestroyOnLoad()
to use this class on other scenes. So the only way to access variable gameManagerScript
- is using function FindGameObjectWithTag
. But when I start the simulation, Find
don't have enought time to find gameManagerObject
. And here I have the error.
Script Execution Order Settings did not helped too. Should I check if the object equals null
?
Class ActsGeneral
void Awake()
{
gameManagerObject = GameObject.FindGameObjectWithTag("GameManager");
gameManagerScript = gameManagerObject.GetComponent<GameManager>();
}
Class Act_0
void Start()
{
// error: Object reference not set to an instance of an object
if (actsGeneral.gameManagerScript.currentActNumber == currentLocalActNumber)
{
}
}
Edit1
I gave Find
function more time to find the object gameManagerObject
, but now I still have error. All objects and component are enabled and I set up Script Execution Order Settings. But it still don't work. I just don't understand the reason of that.
void Start()
{
StartCoroutine("StartDelay");
}
IEnumerator StartDelay()
{
yield return new WaitForSeconds(1.5f);
if (actsGeneral.gameManagerScript != null)
if (actsGeneral.gameManagerScript.currentActNumber == currentLocalActNumber)
{
Debug.Log("222");
}
}