Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by StoneRyder · May 22, 2013 at 03:23 PM · floatstaminachange variable

"Script2" will not update variable in "Script1" (No error messages)

I have a controller script that controls a stamina bar...

 using UnityEngine;
 using System.Collections;
 
 public class StaminaScript : MonoBehaviour {
     
     public Texture2D staminaGuiTex;
     public Texture2D staminaBlueTex;
     public float currTime;
     public static float staminaScale;
     
     // Use this for initialization
     void Awake () {
         currTime = Time.time;
         staminaScale = 221;
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKey (KeyCode.LeftShift) | Input.GetKey (KeyCode.RightShift)){
             if(currTime < Time.time){
                 if(staminaScale != 0){
                     staminaScale -= 0.25f;
                 }
                 currTime = Time.time;
             }
         }
     }
     
     public void OnGUI() {
         GUI.DrawTexture(new Rect(Screen.width - 300, 94, 256, 64), staminaGuiTex, ScaleMode.StretchToFill, true, 0);
         
         GUI.DrawTexture(new Rect(Screen.width - 283, 116, staminaScale, 18), staminaBlueTex, ScaleMode.StretchToFill, true, 0);
     }
 }


and I need this script to change add to it after the victim is attacked...

 <if (((num <= this.biteDistance) && Input.GetButtonDown("Jump")) || (num2 <= this.biteDistance))
         {
             GUIKills.numberOfKills++;
             StaminaScript.staminaScale = (StaminaScript.staminaScale + 221 * (25/221));
             this.status = AIStatus.Attacked;

However, no matter if I use a static variable or not, no error messages appear, but the staminaScale variable doesn't update. Any suggestions?

EDIT: There is a third Script I try to change the variable and it works...

 enter code herevoid Update () 
     {
         if(!died&&(GameController.health <= 0))
         {
             //StaminaScript staminaScript = GameObject.FindGameObjectWithTag("Stam").GetComponent<StaminaScript>();
             StaminaScript.staminaScale = 221;
             Die();    
         }
         if(Input.GetButtonDown("Jump"))
         {
             gameObject.audio.PlayOneShot(audio.clip);    
         }
     }
Comment
Add comment · Show 4
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 ExTheSea · May 22, 2013 at 05:12 PM 0
Share

Are you sure that this line gets executed? Is GUI$$anonymous$$ills.numberOf$$anonymous$$ills increased. If you don't know try to add a Debug.Log right at the beginning of the if statement.

avatar image StoneRyder · May 22, 2013 at 06:28 PM 0
Share

The GUI$$anonymous$$ills.numberOf$$anonymous$$ills does increase. I've also added a print() command to make sure it runs. The code runs good.

avatar image ExTheSea · May 22, 2013 at 06:33 PM 0
Share

What happens if you add Debug.Log(Sta$$anonymous$$aScript.sta$$anonymous$$aScale);?

avatar image StoneRyder · May 22, 2013 at 09:53 PM 0
Share

It doesn't change from before the line to after it. Same if I watch it in the inspector.

To better describe what is going on... When the player holds down shift, he moved faster and the sta$$anonymous$$a drains over time. When he kills an enemy, the sta$$anonymous$$a bar is suppose to refill 25%. If the player dies, the sta$$anonymous$$a bar is refilled completely.

1 Reply

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

Answer by Bunny83 · May 22, 2013 at 06:50 PM

That's simply because this expression evaluates to 0:

 (25/221)

25 and 221 are integer values. When you divide an integer by an integer the result will be also an integer. 25 / 221 is 0.1131 which will be trucated to 0

Use float literals. Either:

 25.0f / 221.0f

or just

 25f / 221f

I prefer the first one because it's more obvious that it's a float.

ps: Never ever compare float values to a certain value unless you set it to this explicit value. This statement will probably never be false:

 if(staminaScale != 0)

use a range instead:

 if(staminaScale > 0)

Also if you don't want it to go below 0 you can clamp it wither with Mathf.Max or if you also have a maximum with Mathf.Clamp

 staminaScale = Mathf.Max(staminaScale, 0.0f); // keep it above or equal to 0
 
 staminaScale = Mathf.Clamp(staminaScale, 0.0f, 100.0f); // keep it between 0 and 100
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 StoneRyder · May 22, 2013 at 09:56 PM 0
Share

Wow, I didn't think of that. At one point at had (float) before all of it, but I didn't think about the individual numbers.

I applied this change and everything is working great. Thanks so much!

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

15 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

Related Questions

Why aren't these numbers going down by 1. 1 Answer

Using Coroutines to increase Float values C# 3 Answers

how to define spesific object in list? 2 Answers

Animator subtracting float to change states in c#? 0 Answers

MouseLook FPS Sensitivty (DropDown) 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