• 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 237641 · Nov 04, 2014 at 07:20 PM · listunityguigui label

GUI.Label not updating image

The health bar has 20 states so I use math.round to find the nearest image to load. I assign the textures in the inspector and I know the script can access it because it works for the initial load but doesn't update thereafter. The code seems to be spot perfect and I can see the healthBar variable that stores the texture changing but the GUI.Label that is getting its image from t$$anonymous$$s variables don't change properly. For example if I set my code to different values beforehand in the code or in the inspector the health bar updates the image properly when I play but it will never change after that even though I can see the healthBar variable updating that the method derives from changing to the right image.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class PlayerControl : MonoBehaviour {
     public int health;
     public int i;
     public List<Texture2D> textures = new List<Texture2D>();
     public Texture2D healthBar;
     // Use t$$anonymous$$s for initialization
     void Awake () 
     {
         health = 10;
         i = 2;
 
     }
     
     // Update is called once per frame
     void Update () 
     {
         if ( health < 0)
             Application.LoadLevel(Application.loadedLevel);
         i =    Mathf.RoundToInt(health / 5);
 
     }
     void OnGUI ()
     {
         healthBar = textures[i];
         GUI.Label(new Rect(0, -50, 128, 128) , healthBar);
     }
 }
 
Comment
Add comment · Show 2
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 troien · Nov 05, 2014 at 09:44 AM 0
Share

When I try your initial code it simply works for me (Well, I did remove the if health 0 thing, and added a i = Mathf.Clamp(i,0,19) after setting i. But I only did that to prevent it from giving me indexoutofrange exceptions and reloading te scene :)

So it's not the code that is your problem I guess...

Maybe you are drawing another healthbar on top of the one you are changing?

avatar image 237641 · Nov 05, 2014 at 08:46 PM 0
Share

Yeah it's possible I was doing that

2 Replies

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

Answer by 237641 · Nov 04, 2014 at 07:33 PM

Well I got it to work like t$$anonymous$$s but t$$anonymous$$s seems a bit ineffiecent to me and still doesn't explain why my previous code wasn't working. Is t$$anonymous$$s another one of those bugs?

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class PlayerControl : MonoBehaviour {
     public int health;
     public int i;
     public List<Texture2D> mores = new List<Texture2D>();
     public Texture2D[] t$$anonymous$$ngs = new Texture2D[21];
     public Texture2D healthBar;
     // Use t$$anonymous$$s for initialization
     void Start () 
     {
         foreach ( Texture2D t$$anonymous$$ng in t$$anonymous$$ngs)
             mores.Add(t$$anonymous$$ng);
         health = 100;
         i = 20;
 
     }
     
     // Update is called once per frame
     void Update () 
     {
         if ( health < 0)
             Application.LoadLevel(Application.loadedLevel);
         i =    Mathf.RoundToInt(health / 5);
 
 
     }
     void OnGUI ()
     {
 
         GUI.Label(new Rect(0, -50, 128, 128) , mores[i]);
     }
 }
 
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
avatar image
0

Answer by lokindia · Nov 05, 2014 at 10:37 AM

@237641: Your 1st code works fine. I tried to run it and healthBar texture was changing as per expectations. I dragged the value of int health from 0 to 99 in the inspector in play mode, the corresponding value of i was changing correctly thus was displaying the correct textures[i] w$$anonymous$$ch you've assigned to healthBar in OnGUI() function.

I'll also suggest you to change the y-coordinate of GUI.Label to a value greater than 0. Your GUI.Label is 128 by 128 but the y-coordinate of top-left edge is -50 thus some part of your texture might be clipping or whole of it if the texture itself is smaller than 50px.

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 237641 · Nov 05, 2014 at 08:44 PM 0
Share

When I was testing the i value changed but the texture wasn't updating. But the second code works fine so I'm not going to spend too much time thinking on this.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

28 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 avatar image

Related Questions

A node in a childnode? 1 Answer

Create a ListView in Unity Android app: use UnityGUI or Android plugin? 0 Answers

Show Top 10 Of 1 GameType 2 Answers

Find GameObjects with a certain Script [Solved] 1 Answer

print the whole list to screen 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