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 /
avatar image
0
Question by $$anonymous$$ · Mar 04, 2019 at 08:40 PM · static variablepublic static

Public static variable not updating from void Start ()?

I have 2 scripts: Score.cs and Scoring.cs. When the public static int iScore is updated in Scoring.cs during void Start (), the variable does not change in Score.cs, although it is fine during void Update ().

Score.cs:

   public class Score : MonoBehaviour
   {
           public Text textbox;
   
           void Start()
           {
                   textbox = GetComponent<Text>();
           }
   
           void Update()
           {
                   textbox.text = "Score: " + Scoring.iScore;
                   Debug.Log (Scoring.iScore);
           }
   }

Scoring.cs:

       public static int iScore;
       private bool bBelow;
       void Start () 
      {
           iScore = 0;
           Debug.Log ("Start" + iScore);
           bBelow = false;
           Vector3 Pos = Movement2.Position;
           this.transform.position = new Vector3 (Random.Range(-2, 2), this.transform.position.y);
      }
   
       void Update () 
      {
           float fPosition = gameObject.transform.position.y;
           Vector3 Pos = Movement2.Position;
           if (Pos.y < this.transform.position.y && bBelow == false)
           {
               iScore = iScore + 1;
               bBelow = true;
           }
           if (fPosition >= 6)
           {
               //Move back to starting position
               this.transform.position = new Vector3 (Random.Range(-2, 2), -6, 0);
               bBelow = false;
           }//end if
      }
  }

Comment
Add comment · Show 2
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 CurtisGM · Mar 04, 2019 at 09:48 PM 0
Share

Hello @$$anonymous$$ ! I opened up a new project in Unity and created 2 scripts named Score.cs and Scoring.cs, assigning both of them to a Text UI element. Your code seems to be running as intended on my end; iScore is updated immediately within the Start function in Score.cs. Is there any other information you could provide to help reproduce this issue?

avatar image RobAnthem · Mar 05, 2019 at 01:59 AM 0
Share

I'm confused, you say your Score never changes in Score.cs Start() function, but why would it? You never change the score there, you only get the reference the to text object. So what seems to be the issue here?

2 Replies

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

Answer by Huacanacha · Mar 04, 2019 at 10:57 PM

The Start method in each script will be executed in arbitrary order so it is likely WarmedxMints is correct - Scoring.Start() is running after Score.Start().


A good rule of thumb is to do internal setup in Awake (like a constructor) and anything with external interactions in Start. That way, for scripts that become active in the same frame, each scripts internal state has always been initialized.


Note: I advise against using Script Execution Order to solve sequencing issues. That creates an external dependency (script order) that is decoupled from the code itself. Static variables are also not advisable for the most part (that's a whole different discussion).

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 $$anonymous$$ · Mar 05, 2019 at 07:58 AM 0
Share

Ok, will try this later, thank you :)

avatar image
1

Answer by WarmedxMints · Mar 04, 2019 at 10:27 PM

This is due to script execution order. Score.cs's Start is being called before Scoring.cs's Start method. You can manually set the script execution order if it is vital the scoring is executed before score.

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

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

DontDestroyOnLoad Question 1 Answer

Acessing static variable from Entities.ForEach 1 Answer

Global Variable Error 1 Answer

why my static timer variable print 0 everytime i call the getcolor function?! 0 Answers

Help with Scripting (Boolean) 1 Answer


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