Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by TheSaviour · Mar 24, 2018 at 07:32 PM · startglobal variables

Is there a difference between initializing a variable in the Start() function and initializing a variable globally?

For example, if I initialize a variable globally, it would look like this:

 public class ExampleOne : MonoBehaviour
 {
    public float x = 10.0f;
 
    void Start()
    {}
 
    void Update()
    {}
 }

But if I initialize a variable in the Start() function, it would be like this:

 public class ExampleTwo : MonoBehaviour
 {
   public float x;

   void Start()
   {
      x = 10.0f;
   }
 
   void Update()
   {}
 }

Is there any difference between these two? Because I played around a bit and I noticed that if I initialize a variable to 10 globally, once I enter play mode, the said variable doesn't start off with the value 10 in the first frame.

But if I initialize the variable in the Start() function instead, the variable starts off with the value 10 right from the first frame once I enter play mode.

Any idea why this is the case? Are the statements in Start() functions somehow prioritized over global statements?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by pophead2 · Mar 24, 2018 at 07:40 PM

Yes. If you initialize it in start, it won't be ready, for example, on Awake() or OnEnable() since they are done before Start();

Comment
Add comment · Show 1 · 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 TheSaviour · Mar 25, 2018 at 08:44 AM 0
Share

Yeah but I'm only referring to Start() function and global variable. $$anonymous$$y script doesn't have Awake() or OnEnable(). When a code is activated, what happens in the first frame? Are the global variables checked first or is the start function checked first?

avatar image
0

Answer by plangiewicz · Mar 24, 2018 at 07:46 PM

if you initialize global float a = 10; then it is initialized before awake(). Difference is if you want to get component then difference is betwen awake() start() onenable etc... because awake is before start and onenable.

If you have

class something{

anotherClass another;

then you should init this another in awake()

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
avatar image
0

Answer by sisse008 · Mar 25, 2018 at 11:08 AM

TheSaviour, you do not create Awake, Enable, and start. they are names of time instances. your Update function is called x FPS. even if you did not have an update function, your game would still run at x FPS.

if you have a function Start() in your script than it is called a short time after your game begins. if you have an Awake() function than it will be called at an even shorter time after your game begins. there for, it is safer.

if a different script in your game is running and that script has

 void Awake()
 {
       x++;
 }

then you will get an error because x is being set in the Start() function and start function is called after Awake().

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 TheSaviour · Mar 25, 2018 at 03:57 PM 0
Share

Alright I get it. But what about global variables that I assign before the Start() or Awake() function? When my game starts, are those variables checked before the Awake() or Start() function?

avatar image sisse008 TheSaviour · Mar 25, 2018 at 04:05 PM 0
Share

yes. you can do a simple check

 int x =1;
 
 void Start()
 {
      Debug.Log("x = " + x);
 }
avatar image
0

Answer by Glurth · Mar 25, 2018 at 05:16 PM

When you create a member variable in your Monobehavior class

 public int a=1;

The value 1 will be assigned to integer "a" when the object is INSTANTIATED (created / put in the scene). The game need not even be launched (played) for this value to be assigned (unlike Start). As soon as your behavior is put on an object, in the editor, this value will be assigned.

Once the object is instantiated, the value is saved, by unity serializing the Monobehavior object and saving it with the scene. When the scene is re-loaded, this serialized value will be re-assigned to the variable. If the scene is loaded becaue the game has just been started, Start() will be called AFTER the serialized values have been reassigned.

You can optionally use the Reset() function, to assign these values (what I would recommend). This also works in the editor, and does not require the game actually be running. This way when you select "reset" from the object's editor menu, the object ACTS like it has just-now been instantiated.

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

78 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 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 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 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 to get Unity to ask what project to load every startup? 1 Answer

Making a start screen in Unity 3 Answers

DontDestroyOnLoad() not calling awake/start again 2 Answers

Object reference becomes null between Awake and Start after scene load 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