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 Bio Discus · Dec 24, 2013 at 05:41 PM · c#variable

Variables not setting correctly

Hello,

I've got a few variables

 public int health; 
 private int test = 10;

Changing it in the Start function

 public void Start()
 {
     test = health;
     Debug.Log("test: " + test);
 }

Which displays: 1, as intended. But later when I try to log the variable again, I get an incorrect number.

 public void Touch()
 {
     Debug.Log("test: " + test);
 }

Displays: 10.

If I change this variable to 20. The Touch function will still display 10, until I re-open Unity.

What is happening...?

Comment
Add comment · Show 6
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 robertbu · Dec 24, 2013 at 05:45 PM 0
Share

Are you sure this is your exact code? The problem you describe would happen if on 3 in your start section you had this ins$$anonymous$$d:

 int test = health;
avatar image azmat786n · Dec 24, 2013 at 06:41 PM 0
Share
 public int health = 100;
 private int test = 10;
 
 void Start() {
     //changing test value to health
     test = health
 
     //print log
     print("Health is about " + test.ToString());
     //Output should be (Health is about 100)
     //whenever you print it its value same as health
 }
avatar image felixpk · Dec 24, 2013 at 06:51 PM 0
Share

If the variable is public, it will allways get the int you setted in the inspector.

avatar image Bio Discus · Dec 24, 2013 at 08:15 PM 0
Share

The full class:

 public class $$anonymous$$oleObject: $$anonymous$$onoBehaviour
 {
     public int health;
     private int test = 200;
     
     public void Start()
     {
         test = health;
         Debug.Log("Test 2: " + test);
     }
 
     public void Touch(int mod)
     {
         Debug.Log("Test: " + test);
     }
 }

The variable 'test' was set on the first run, changing the variable later won't change anything.

Here is a screenshot: http://gyazo.com/dc86717e1cd4efa2a2622c924866e6d9.png

avatar image Benproductions1 · Dec 25, 2013 at 09:27 AM 0
Share

Note that changing health in the inspector won't change test after that Start was called. integers are passed by value, not by reference

Show more comments

2 Replies

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

Answer by 0Zidane0 · Jun 20, 2014 at 03:31 PM

I ran into a similar situation just now. It turned out I was only changing my public variable on a single instance of a prefabbed object in the editor, when I wanted to change it on the prefab itself, leading to a lot of confusion. Is this perhaps what you're doing as well?

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 Bio Discus · Jun 20, 2014 at 04:09 PM 0
Share

This was a while ago, sorry for not answering how I fixed it. But my problem was that I was setting the variable of the prefab ins$$anonymous$$d of the create object. This caused unwanted behaviour. Why this behaviour happend I don't know, it could be a bug in Unity.

I'm going to mark this as the answer, so people know what the solution is :)

avatar image
0

Answer by TheRichardGamer · Dec 25, 2013 at 07:47 PM

Since your health variable is empty, the test variable doesn't know what to be set to, so you'll need to enter a value in the health variable for it to work.

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 Bio Discus · Dec 27, 2013 at 12:35 PM 0
Share

The health variable is set in the unity editor. But that's not the point... I already set in the post that health and test output the correct number: 1. But after the start function test outputs a completely number.

avatar image Benproductions1 · Dec 27, 2013 at 12:44 PM 0
Share

@Bio Discus, sorry but that last sentence doesn't make any sense. Could you try reformulating your sentence or if you are having trouble translating from a different language write both what you want to say in your mother tongue and google-translate's result?

avatar image Bio Discus · Dec 27, 2013 at 01:04 PM 0
Share

Sorry for that.

The health variable is set in the unity editor to 1. The test variable is set to the health variable in the Start function, this logs: 1 (test = health). But in the Touch function the test variable behaves weirdly, it outputs a completely different number (See this image: http://gyazo.com/dc86717e1cd4efa2a2622c924866e6d9.png).

I just checked the log inside the Update function, and everything logs as it should be logged. Is it possible there is a problem with the function: GetComponent(); ?

avatar image KellyThomas · Dec 27, 2013 at 01:25 PM 0
Share

Please show us the code you are using for your GetComponent() call.

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

24 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Loading a Constant Array Of Integers 0 Answers

Debug.Log a gameobject in C ? 2 Answers

How do I make my own Remote Settings backend like Unity Analytics Remote Settings? 0 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