Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by ansheng · Jun 08, 2014 at 03:17 PM · c#variablestart

What is the difference between a variable instantiated inside a method(specifically inside void Start()) and one not inside any methods?

Just finished a project, and realize I'm confused with these two things:

 int life = 10;

and

 int life;

 void Start(){
     life = 10;
 }

usually, if one doesn't work as I expected, I just do the other one. I also notice on the tutorials I found that when they declare a List or Script, they always instantiate it inside Start()

 List <GameObject> stages;

 void Start(){
     stages = new List<GameObject>();
 }

Hope to know the difference if there are, including on the event when I moved from one scene to another. For example, if I click a button that has a script like this:

 OnMouseDown(){
     //scene 1 has the int life;
     Application.Load("Scene1");
     //use static variable Player to access int life;
     Player.life = 3;

}

what is the value of life 0, 10 or 3;

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

2 Replies

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

Answer by PouletFrit · Jun 08, 2014 at 04:03 PM

Initializing a variable outside of Start or Awake will simply initialize that variable at the moment the constructor for that object is called. If your object inherit from MonoBehaviour the constructor get called when the object is created/loaded. Then once the serialized data is restored (if needed) to the object, Unity goes through Awake function then throught the Start function.

So pretty much it's only a matter of when you want that variable to be initiated.

Constructor (which is kind of hidden in unity) --> Awake --> Start

And life should end up having a value of 10. Unity load the level at the next frame, so it will finish the current function and the Player.life = 3 will be overriden by the Start/Awake functions

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 ansheng · Jun 16, 2014 at 02:42 PM 0
Share

but when I put int life = 10 outside start or awake, that is I instantiate it above, then life will be 3, right?

avatar image meat5000 ♦ · Jun 16, 2014 at 02:51 PM 1
Share

Declare - Assign memory of certain layout (and size).

Instantiate - Assign an initial value.

You may instantiate during declaration if you wish. It will be a default value and is sometimes good practice to avoid nulls and nonsense. Do this outside functions to extend the scope of that variable for use in the whole script.

You may Declare without Instantiating. You are simply reserving a chunk of memory of a certain layout, but not giving it a value. You may then assign a value to this variable anywhere in the script and the var will retain that value through the whole script.

$$anonymous$$ost people perform Instantiation during Start() to ensure that the program has run enough for the relevant objects etc to exist, avoiding nulls. If you Declare during Start() that variable will only have scope inside that function and will not exist outside of that function.

avatar image
0

Answer by robusto · Jun 08, 2014 at 03:49 PM

http://unity3d.com/learn/tutorials/modules/beginner/scripting/awake-and-start http://docs.unity3d.com/Manual/ExecutionOrder.html (look at the graph)

Comment
Add comment · 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

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Initialising List array for use in a custom Editor 1 Answer

How do i start a game with Unity, what do i need first? 0 Answers

C# Accessing Variables from Class Variable 1 Answer

C# Referencing a Non-Static Variable Help 2 Answers

How do i access a variable in one script from another script? 2 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