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 Hydropulse17 · Jul 09, 2014 at 12:59 AM · renderingvariableupdatecall

How to change the rate of decrease in a variable in the update function

My truck has fuel, and I wanted it to decrease slowly. In side a function I defined to draw the fuel gauge, which is called in the update function, I had this simple line: gameLevel_001_Fuel -= 0.5. Everything worked fine, the gauge in the game displayed the right amount of fuel, but it drained WAY TOO QUICKLY. Simple fix I thought, just make the value smaller. Only problem was that when I changed 0.5, to 0.05, the rate at which it decreased didn't change. I tried 0.000005, same rate. I even tried calling it directly in in the update function. Maybe there's a better way to have it decrease as time passes?

 function SetGuages() { //Called immediately in update function
     
     gameLevel_001_Fuel -= 0.05;
     
     if (gameLevel_001_Fuel > 94){
         fuelGuage.renderer.material = fuel_100;
     }
     else if (gameLevel_001_Fuel > 87){
         fuelGuage.renderer.material = fuel_094;
     }
     else if (gameLevel_001_Fuel > 81){
         fuelGuage.renderer.material = fuel_087;
     }
     else if (gameLevel_001_Fuel > 75){
         fuelGuage.renderer.material = fuel_081;
     }
     else if (gameLevel_001_Fuel > 69){
         fuelGuage.renderer.material = fuel_075;
     }
     else if (gameLevel_001_Fuel > 62){
         fuelGuage.renderer.material = fuel_069;
     }
     else if (gameLevel_001_Fuel > 56){
         fuelGuage.renderer.material = fuel_062;
     }
     else if (gameLevel_001_Fuel > 50){
         fuelGuage.renderer.material = fuel_056;
     }
     else if (gameLevel_001_Fuel > 44){
         fuelGuage.renderer.material = fuel_050;
     }
     else if (gameLevel_001_Fuel > 37){
         fuelGuage.renderer.material = fuel_044;
     }
     else if (gameLevel_001_Fuel > 31){
         fuelGuage.renderer.material = fuel_037;
     }
     else if (gameLevel_001_Fuel > 25){
         fuelGuage.renderer.material = fuel_031;
     }
     else if (gameLevel_001_Fuel > 19){
         fuelGuage.renderer.material = fuel_025;
     }
     else if (gameLevel_001_Fuel > 12){
         fuelGuage.renderer.material = fuel_019;
     }
     else if (gameLevel_001_Fuel > 6){
         fuelGuage.renderer.material = fuel_012;
     }
     else if (gameLevel_001_Fuel > 0){
         fuelGuage.renderer.material = fuel_006;
     }
     
     
 }
Comment
Add comment · Show 1
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 SirCrazyNugget · Jul 09, 2014 at 01:05 AM 0
Share

That's a whole lot of IFs and you're setting the material even when it's not changed!

Place your materials in an array:

 var fuel$$anonymous$$aterials[];

Create two ints:

 var required$$anonymous$$aterial : int;
 var current$$anonymous$$aterial : int;

Use a little math to deter$$anonymous$$e which material to show:

 required$$anonymous$$aterial = int(materialgameLevel_001_Fuel / 6);

Only change the material when needed:

 if(current$$anonymous$$aterial != required$$anonymous$$aterial){
   fuelGuage.renderer.material = fuel$$anonymous$$aterials[required$$anonymous$$aterial];
   current$$anonymous$$aterial = required$$anonymous$$aterial;
 }

This is only pseudo code! You'll probably need to change a line or too.

1 Reply

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

Answer by Hydropulse17 · Jul 09, 2014 at 12:44 AM

NVM, I'm a dunce... It's because gameLevel_001_Fuel was an int variable, not a float. God I'm ashamed... I'll leave this up though, so people even less experienced than me can see how crucial it is to use the right var types.

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

2 People are following this question.

avatar image avatar image

Related Questions

Call a method whenever the assigned variable gets altered 2 Answers

Can I use RPC calls in an Update function ? 1 Answer

Why a public varibale is not accsible inside Update() ? 1 Answer

How do i call a variable from another script? 1 Answer

Variable updates everywhere but Update function. 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