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 AKJ9 · Aug 28, 2014 at 01:23 PM · c#materialcolorguitext

Changing GUI Text colour when health is low

Trying to get my GUI health text to change colour to red when it's below 40. Seen so many 'methods' of doing this, but can't get it to work. Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class HealthController : MonoBehaviour 
 {
     public static HealthController Instance { get; private set; }
     public int health;
     public GUIText healthTXT;
     public GUIText healthTXT2;
     public AudioClip shotSound;
     public AudioClip dieSound;
     public AnimationClip die;
     public AnimationClip hit;
     
     void Awake()
     {
         if (Instance == null) 
         {
             Instance = this;
         }
         else
         {
             Destroy(gameObject);
         }
     }
     
     void Start() 
     {
         health = 100;
         UpdateHealthText();
     }
 
     public void HasBeenAttacked20()
     {
         if (health <= 0) return;
         
         health -= 20;
         
         UpdateHealthText();
         
         if (health <= 0) {
             StartCoroutine(Die());
         }
         else
         {
             audio.PlayOneShot(shotSound);
             animation.Play("hit");
         }
     }
     public void HasBeenAttacked10()
     {
         if (health <= 0) return;
         
         health -= 10;
         
         UpdateHealthText();
 
         if (health <= 0) {
             StartCoroutine(Die());
         }
         else
         {
             audio.PlayOneShot(shotSound);
             animation.Play("hit");
         }
     }
     private void redText(){
     if (health <= 40) 
         healthTXT.material.color = Color.red;
         
     }
 
     private void UpdateHealthText()
     {
         healthTXT.text = "LIFE: " + health.ToString();
         healthTXT2.text = "LIFE: " + health.ToString();
 
     }
 
     private IEnumerator Die()
     {      
         Motor.Instance.enabled = false;
         GetComponent<CharacterAnimationController> ().enabled = false;
         animation.Play("die");
         audio.PlayOneShot(dieSound);
         yield return new WaitForSeconds(die.length);
     }
 }

The redText() method is the one we're concerned with, I can't see why that doesn't work. Please let me know what I'm doing wrong and advise on an alternative solution. Please bear in mind that healthTXT2 is simply the shadow of healthTXT.

Thanks for your time and support in advance.

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

1 Reply

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

Answer by MrMatthias · Aug 28, 2014 at 01:35 PM

Do you actually call the redText method?

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 AKJ9 · Aug 28, 2014 at 02:23 PM 0
Share

You're a star. I did try to call it before, but by you saying that I realised I didn't call it in both HasBeenAttacked10 and HasBeenAttacked20 methods. You sparked a brainwave. Thank you!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Changing two different objects renderer colour 1 Answer

Material doesn't have a color property '_Color' 4 Answers

Animating GUIText material properties 0 Answers

Assigning HDR color to material property via script 4 Answers

Distribute terrain in zones 3 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