Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ObviouslyInsane · Aug 28, 2011 at 04:54 AM · updatetimetimershieldhealth

Update not being called every frame

Hi everyone, I have a script here that is supposed to regenerate my shields if the curShield is less than maxShield. It works but it only calls the AdjustCurrentShield function when I am moving, it wont work if I am just standing still. Can someone take a look and tell me where I went wrong?

 static var maxHealth : int = 100;
 static var curHealth : int = 100;
 
 static var curShield : int = 100;
 static var maxShield : int = 100;
 
 static var adjs : int;
 static var adj : int;
 
 var shieldText : Texture2D;
 var healthText : Texture2D;    
 
 var healthBarLength : float;
 var shieldBarLength : float;
 
 var shieldRegen : float = 5;
 
     // Use this for initialization
 
     function Start() {
         healthBarLength = 250;
         shieldBarLength = 250;
 
     }
 
     // Update is called once per frame
 
     function Update() {
 
         AdjustCurrentShield();
         AdjustCurrentHealth();
 
     }
 
     function OnGUI() {
 
          GUI.DrawTexture(new Rect(10, 10, shieldBarLength, 24), shieldText);
          GUI.DrawTexture(new Rect(10, 36, healthBarLength, 24), healthText);    
 
     }
 
     function AdjustCurrentHealth() {
         curHealth += adj;
 
         if(curHealth <= 0)
         Destroy(gameObject);    
 
         if(curHealth > maxHealth)
             curHealth = maxHealth;
 
         healthBarLength = 400 * curHealth / maxHealth;
         
 
         }    
 
     function AdjustCurrentShield() {
         curShield += shieldRegen * Time.deltaTime;
 
         if(curShield < maxShield)
             curShield += shieldRegen * Time.deltaTime;
 
         if(curShield > maxShield)
             curShield = maxShield;
 
         if(maxShield < 1)
             maxShield = 1;                     
 
         shieldBarLength = 400 * curShield / maxShield;
 
         }
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

2 Replies

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

Answer by eem · Aug 28, 2011 at 09:46 AM

Thats not possible. You AdjustCurrentShield function is getting called every frame.

A good technique for debugging is to lay Debug.Log calls throughout your script.

So at the begenning of your AdjustCurrentShield function put:

Debug.Log("Adjust Current Shield Called!");

this will print out a message so you can see it being called every frame.

Now if a variable or something is not reacting as you expect, add a Debug.Log call to watch that variable.

for example... Debug.Log(curShield);

then see if the variable is outputting what you expect

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 ObviouslyInsane · Aug 28, 2011 at 11:28 PM 0
Share

Ok so I added the debug statements and they ARE being called but the shield regen still isn't working. So do you see anything wrong the code that might be causing it to not work?

avatar image
1

Answer by hodev · Jan 26, 2013 at 04:12 AM

OK this is a pretty old post, but I landed on it and there's still no answer, so I thought I'd answer..

Regarding this line: curShield += shieldRegen * Time.deltaTime

curShield is an int, and you're adding a float that is being multiplied by Time.deltaTime. It is possible that the result of 'shieldRegen * Time.deltaTime' is less than 1, and so when added to an int, adds zero.

Maybe while your character is moving, it drops the framerate enough (increase Time.deltaTime) to bump the result over 1, thereby suddenly working.

Changing curShield to a float should fix the problem. Then you might want to round shieldBarLength before assigning it.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to destroy in an amount of time 1 Answer

Minute Timer Issue 1 Answer

Time based scoring 2 Answers

Decrease Health every 5 mins or Time script? 2 Answers

Accumulating deltaTime oddity 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