- Home /
This Question is invalid - user error
Cached references set in awake aren't persisting
Hi. I have an interesting bug probably resulting in an over sighted "gotcha".
I have a script "A" with some variables
Material myMaterial; // private material reference
public Renderer myRenderer; // public renderer reference assigned in inspector
Its awake function:
void Awake()
{
myMaterial = myRenderer.material;
Debug.Log(myMaterial); // this is not null
}
It has another function called DoStuff
public void DoStuff()
{
Debug.Log(myMaterial); // this shows as null (details below)
myMaterial.color = new Color(blablabla);
}
Then I have another script "B" that accesses DoStuff in its Start function
void Start()
{
aScript.DoStuff(); // I get an error that myMaterial is not set in "A"
}
more details: i have 2 scenes that make use of these two scripts.. in one scene i get no error but i get the error in the other scene because the script execution order seems to be different.
but script order execution shouldn't matter because all Awake() functions are called before any Start() functions.
So the order in the scene where there is the reference error is as follows:
A.Awake()
B.Awake()
A.Start()
B.Start()
in the scene where there is no bug uses the following order
B.Awake()
A.Awake()
B.Start()
A.Start()
Follow this Question
Related Questions
Awake called after I activate object, not after Instantiate... is it normal?? 2 Answers
Awake/Start order across different platforms. 3 Answers
ExecuteInEditMode Component construct "twice" and "reset" custom classes 0 Answers
Execution Order of Scripts - Awake and OnEnable 1 Answer
Timer that stops at the end of the game 3 Answers