Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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.

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

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

Start is not called after reloading the level 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges