Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

Cached references set in awake aren't persisting

$
0
0
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()

Viewing all articles
Browse latest Browse all 171066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>