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 Giobar97 · Jul 10, 2014 at 11:02 AM · c#guivariableupdate

GUI Variables doesn't update C#

Hi, I'm stuck with this problem and I can't resolve it.

I'm trying to make a healt-bar which diplays both health and lives. They are controlled by two simple integers. When I try to change these integers from other scripts, they actually change in the inspector, but they don't in the GUI. For example, if I have 3 lives, and I add a life from another script, I still see 3 in game, but if I pause the game and check in the inspector I see 4. Another weird thing is that if I stop playing the game, and start again, it shows 4 in the GUI! Same thing happens with energy bar, while I have other variables shown in the GUI which uptade normally.

There is the code of the GUI.

 using UnityEngine;
 using System.Collections;
 
 public class HUD : MonoBehaviour {
 
     public Texture2D cursore;
     public Texture2D cube;
     public Texture2D hammer;
     public Texture2D plane;
     public Texture2D magnete;
     public Texture2D shoes;
     public Texture2D life;
     public Texture2D fullbar;
     public Texture2D emptybar;
     public int numc;
     public int numh;
     public int nump;
     public string strc;
     public string strh;
     public string strp;
     public string strlif;
     public GUIStyle style;
     public bool magnet;
     public bool shovel;
     public bool portalgun;
     public bool shoe;
     public Texture2D pogu;
     public Texture2D pala;
     public GameObject ply;
     public CharacterMotor cmj;
     public int nrg;
     public int lifes;
 
     void Start(){
         Screen.showCursor = false;
         style.fontSize = 20;
         style.alignment = TextAnchor.UpperCenter;
         cmj = ply.GetComponent<CharacterMotor>();
     }
 
     void Update(){
         strc = numc.ToString();
         strh = numh.ToString();
         strp = nump.ToString();
         strlif = lifes.ToString();
     }
 
     void OnGUI(){
         GUI.DrawTexture (new Rect(5, 5, 100, 100), life);
         GUI.DrawTexture (new Rect(100, 53, 500, 50), emptybar);
         GUI.DrawTexture (new Rect(100, 53, nrg, 50), fullbar);
         GUI.DrawTexture (new Rect((Screen.width/2)-24, (Screen.height/2)-24,48,48), cursore);
         GUI.DrawTexture (new Rect(Screen.width - 75, 1, 71, 71), hammer);
         GUI.DrawTexture (new Rect(Screen.width - 146, 1, 71, 71), cube);
         GUI.DrawTexture (new Rect(Screen.width - 217, 1, 71, 71), plane);
         GUI.Label (new Rect(Screen.width - 49, 5, 20, 40), strh, style);
         GUI.Label (new Rect(Screen.width - 118, 5, 20, 40), strc, style);
         GUI.Label (new Rect(Screen.width - 188, 5, 20, 40), strp, style);
         GUI.Label (new Rect(43, 43, 20, 40), strlif, style);
         if (magnet == true){
             GUI.Label (new Rect(Screen.width - 298, 1, 71, 71), magnete);
         }
         if (shovel == true){
             GUI.Label (new Rect(Screen.width - 362, 1, 71, 71), pala);
         }
         if (portalgun == true){
             GUI.Label (new Rect(Screen.width - 447, 1, 71, 71), pogu);
         }
         if (shoe == true){
             cmj.jumping.extraHeight = 4.1f;
             GUI.Label (new Rect(Screen.width - 518, 1, 71, 71), shoes);
         }
     }
 }

The variables I'm talking about are "nrg" and "lifes".

Hope someone can help me. I'm sorry if I wrote something wrong, but I'm Italian :D

Comment
Add comment · Show 12
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 Fornoreason1000 · Jul 10, 2014 at 11:13 AM 0
Share

here's a though why don't you try using Debug.Log on strlif? does it change along (which it should ) with lifes?

  void Update(){
         strc = numc.ToString();
         strh = numh.ToString();
         strp = nump.ToString();
         strlif = lifes.ToString();
      //be sure to have the "collapse" option checked in the console!!
        Debug.Log(strlif);
     }
avatar image Giobar97 · Jul 10, 2014 at 11:21 AM 0
Share

I set 3 at the start and subtracted 1 in game. Debug still shows "3", but the inspector shows 2 correctly.

avatar image Fornoreason1000 · Jul 10, 2014 at 11:36 AM 0
Share

then this strlif = lifes.ToString() isn't working... which is incredibly strange.... since ToString() is a default system function and has nothing to do with unity or its GUI.

this means your GUI is probably working but your strlife is not being updated for some reason.

try this one ins$$anonymous$$d see what that does... it will debug lifes.. so we know for sure its updating.

  void Update(){
         strc = numc.ToString();
         strh = numh.ToString();
         strp = nump.ToString();
         strlif = lifes.ToString();
      //be sure to have the "collapse" option checked in the console!!
        Debug.Log(lifes);
     }

also make sure you don't have any red errors.. they halt the script execution...

avatar image Landern · Jul 10, 2014 at 11:47 AM 1
Share

update is once per frame, OnGUI is multiple/per event. As a test, put lifes.ToString() in the label and replace the variable strlif, and evaluate what is happening then.

avatar image Landern · Jul 10, 2014 at 12:09 PM 1
Share

@Fornoreason1000 no one is arguing that, but we are trying to isolate the issue.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

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

Display additional text before the variable that the user is editing in a GUI Text Field 1 Answer

summing up items price in runtime 1 Answer

Stop checking for values in Update function? 2 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