Variables being initialized BEFORE Awake ()?? How is it possible?
This is driving me crazy, and I'm not being able to debug it!!
A few variables declared in a class I've created for a game I'm developing are already initialized IN THE FIRST LINE OF AWAKE, before any code is run in that class!! How is that possible at all? I don't know what I have done wrong, but as an example I have declared an int variable [HideInInspector] public int m_NumberOfPlayers, which is set to "3" and also an Array called [HideInInspector] public PlayerCap [] m_PlayerCaps, which already has a count of "3" when Awake () kicks off.
How is this even possible? Can someone give me a clue or even some kind of hint on how I can debug this?
Answer by tanoshimi · Nov 20, 2016 at 09:30 PM
public
variables are initialised with values assigned in the inspector, not with values set in code. Take off the [HideInInspector]
decoration and you'll be able to change that value.
...and if you look in the docs, HideInInspector variables really are serialized. This seems to make no sense, since why would you want them saved if they can't be set in the first place? But you could have an Editor script secretely using them.
Wow. @tanoshimi can't thank you enough for your help. This was driving me nuts. The reason I declared these variables as public
in the first place was because I needed to access them from other scripts. Since I didn't need to set them in the editor, I then added the [HideInInspector]
modifier. What I was not aware is that once you set a variable to a value in the editor, even if you later on use the [HideInInspector]
modifier and never see the variable again in the editor, it will keep the initial value it was set to anyway. Crazy! Again, kudos for your help!!!
Your answer
Follow this Question
Related Questions
Access scripts on Awake or Start? 1 Answer
Timer starts from wrong value on void Start or Awake? 1 Answer
Do things before all other scripts started 1 Answer
Access singleton in OnEnable when it's initialized in Awake 3 Answers
Execution order 0 Answers