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 CB-TV · Jun 21, 2015 at 09:57 AM · bugvariableglitchincreaseprint

Variable not Updating

I have create a private variable in my script. I have set its value to 0. It is not in the inspector because it is private. But yet, when I apply this script:

 function Update ()
 {
     print (thirst);
     
     if (thirst < 100)
     {
         thirst = thirstRate * Time.deltaTime;
     }
 }

the variable remains 0 when printing it. This makes no sense but I must be doing something wrong...or it could be a glitch with Unity. Please help if you know anything! Am I putting this in a wrong function or something? Thanks!

Whole script:

 #pragma strict
 
 //Player variables
 
 var thirst = 0; //100 = max, 0 = min.
 var thirstRate = 0.08; //Increase thirst by...
 
 function Update ()
 {
 
     //thirst
     print (thirst);
     
     if (thirst < 100)
     { 
         thirst = thirstRate * Time.deltaTime;
     }
 }
Comment
Add comment · Show 7
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 zach-r-d · Jun 21, 2015 at 12:18 PM 1
Share

Could you post the whole script that this update function is in?

avatar image Priyanshu · Jun 21, 2015 at 01:12 PM 0
Share

is your thirstRate = 0 by any chance? :P

to see private variables in Editor. Either select Debug $$anonymous$$ode in the drop down located at the top right corner of Inspector window. Or assign [Serializable] attribute to your private var.

avatar image CB-TV · Jun 21, 2015 at 03:18 PM 0
Share

zach.r.d, I've added my full script.

avatar image Scribe · Jun 21, 2015 at 03:43 PM 0
Share

you need thirst to be a float type, so either do:

 var thirst = 0.0;

or

 var thirst : float = 0;
avatar image hoekkii · Jun 21, 2015 at 04:57 PM 0
Share

thirst is now an int. and Time.deltaTime is almost always below 1, so (int)Time.deltaTime = 0. thats why thirst does not increas.

the solution is using a float

Show more comments

2 Replies

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

Answer by Flightkick · Jun 21, 2015 at 05:37 PM

It's because you keep dividing the thirst.

Time.deltaTime is the time between each frame (and thus Update() call).

 thirst = thirstRate * Time.deltaTime;

Will result in a very small result. I think you might be trying to increase thirst over time, if so you should try this instead:

 thirst += thirstRate * Time.deltaTime;

Note the += instead of =

EDIT:

Like @Bunny83 mentions, the datatypes should also be defined as float because operations on the data will be rounded down. (i.e. 1 * 1.5 = 1 when using integers).

Comment
Add comment · Show 3 · 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 Bunny83 · Jun 21, 2015 at 05:42 PM 2
Share

This and in addition the variable is an int variable. So it will most likely not change at all since the changed will be smaller than 1.

The "thirst" variable should be declared as float ins$$anonymous$$d of int.

Like this:

 var thirst = 0.0; 

ins$$anonymous$$d of

 var thirst = 0; 

Another case where UnityScript causes more confusion in beginner hands...

avatar image Flightkick · Jun 21, 2015 at 05:47 PM 0
Share

@Bunny83, Indeed, I didn't see that at first (used to C# with explicit na$$anonymous$$g conventions, I like to see the explicit datatype directly ins$$anonymous$$d of looking at the rest of the code to figure out what it holds). Thanks for adding, I'll update my answer.

avatar image CB-TV · Jun 21, 2015 at 08:50 PM 0
Share

Thanks everyone with the help, I knew it was something simple like that!

avatar image
1

Answer by Socapex · Jun 21, 2015 at 12:34 PM

Maybe an int conversion? If the rate is too low it will floor to 0. Try thirst = (float)thirstRate * Time.deltaTime; just in case.

Also, a life changer thanks to TheAntRanch on twitter, if you put the editor in debug mode, you can see your private variables :O

Its really hidden, in the top top right drop down :)

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

27 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

Related Questions

Cant see Point light and i see black things which shouldn't be there? 1 Answer

Is this a Glitch/bug or is it normal? 1 Answer

Increase a variable of an other script 2 Answers

Object Changing Y Coordinate in Game View 0 Answers

Variable suddenly increases with time.deltatime 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