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
0
Question by gdubrocks · May 07, 2014 at 06:19 PM · c#variables

Script variable can be referenced in Start but not in update.

Hello, I have a two part question. First, I can print a gameObjects speed fine in the method where it was initialized, but when I follow the exact same process inside update it prints 0. Why would this be?

Secondly I was told that it's a bad idea to be calling getObjects() for the stats script every frame because it was an expensive operation and that I should instead look into making a global variable for the scripts. The problem I have with this is that I sort the gameobjects by speed every single turn, so the scripts location might not be easy to access. I thought about storing the script with the gameObject in a "pair" but I don't think that that is the right solution either.

 public PlayerStats playerStats;
 List<GameObject> players = new List<GameObject>();
 
 void Start()
     {
         //create players
         GameObject player1 = GameObject.CreatePrimitive(PrimitiveType.Capsule);
         player1.transform.position = new Vector3(4,1,0);
         PlayerStats player1Stats = player1.AddComponent<PlayerStats>();
         player1.tag = "Player";
         player1Stats.setSpeed (15.0f);
         print(player1Stats.speed);
                 //This line prints 15, as expected
         
         GameObject player2 = GameObject.CreatePrimitive(PrimitiveType.Capsule);
         player2.transform.position = new Vector3(0,1,4);
         PlayerStats player2Stats = player2.AddComponent<PlayerStats>();
         player2.tag = "Player";
         player2Stats.setSpeed (10.0f);
         print(player2Stats.speed);
                 //this line prints 10, as expected
         
         players.Add(player1);
         players.Add(player2);
         
         PlayerStats ps;
         ps = players[0].GetComponent<PlayerStats>();
         print(ps.speed);
                 //this line prints 15, as expected
                 
     }
 
     // Update is called once per frame
     void Update () {        
         int playerSize = players.Count;
         int index = 0;
         PlayerStats ps;
         
         while(index < playerSize)
         {
             ps = players[index].GetComponent<PlayerStats>();
             print(ps.speed);
                         //this line ALWAYS prints 0, when it should print either 10 or                  
                         //15.
             index++;
         }
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
1
Best Answer

Answer by Loius · May 07, 2014 at 06:49 PM

Is playerstats initializing itself or doing any logic of its own?

Comment
Add comment · Show 4 · 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 gdubrocks · May 07, 2014 at 07:20 PM 0
Share

No. Playerstats is attached to the players when they are initialized. Right now it just stores the variables as well as getter/setter methods(which are not needed as the variables are currently public).

avatar image gdubrocks · May 07, 2014 at 07:40 PM 0
Share

You had the right idea Loius.

I was initializing the variables within playerStats as well as here, which caused them to be overridden.

Thanks.

avatar image gdubrocks · May 07, 2014 at 08:01 PM 0
Share

Loius answered the question with his comment, I am not sure how to close this.

avatar image Loius · May 07, 2014 at 11:13 PM 0
Share

$$anonymous$$arking an answer as correct "closes" the question good enough, there's no need to actually close it. :)

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

Accessing variables from scripts issues 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Problem with var in C# (Vector3, transform and similiar) 1 Answer

Convert from javascript to c# 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