• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by tugacoder · Nov 20, 2016 at 09:28 PM · variablestartawakeinitializeorder-of-execution

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Nov 20, 2016 at 09:41 PM 1
Share

...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.

avatar image tugacoder · Nov 22, 2016 at 10:49 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Access scripts on Awake or Start? 1 Answer

Is there a way to assign variables before Awake() is triggered 3 Answers

Access singleton in OnEnable when it's initialized in Awake 3 Answers

Execution order 0 Answers

Do things before all other scripts started 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges