Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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
3
Question by mayu_polo · Dec 23, 2014 at 07:14 PM · uihow-tonew ui

Unity 4.6 How to change slider color via script

Hi, I am trying to make a sort of health bar with a slider from the new UI system. I wanted to get fancy and have the slider background color start as green and then change to red once the health is below a certain threshold. However I cannot figure out how to change the background color of the Fill Area of the slider via code. I did some googling but no luck so far.

Thanks!

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
7

Answer by Mexallon · Dec 23, 2014 at 07:25 PM

What you want is to change the Color of the Image component of the "Background" Child of the Slider and set it to red. And the Slider->Fill Area->Fill Child has also an Image Compinent with an Attribute called Color that you want to set to green for example.

Comment
Add comment · Show 5 · 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 Mexallon · Dec 23, 2014 at 07:33 PM 1
Share

You could use Color.Lerp if you want to fade between green and red as the health goes down.

avatar image Mexallon · Dec 23, 2014 at 10:19 PM 2
Share

I've tried it out this is an example for the use of Color.Lerp to get a smooth change from red health bar to green as I want it but you are free to change it that it only changes it color if its below 1/3 health or similar:

 public class HealthBar : $$anonymous$$onoBehaviour {
 
     private Slider slider;
     private int counter;
 
     public int $$anonymous$$axHealth = 500;
     public Image Fill;  // assign in the editor the "Fill"
     public Color $$anonymous$$axHealthColor = Color.green;
     public Color $$anonymous$$inHealthColor = Color.red;
 
     private void Awake() {
         slider = gameObject.GetComponent<Slider>();
         counter = $$anonymous$$axHealth;            // just for testing purposes
     }
 
     private void Start() {
         slider.wholeNumbers = true;        // I dont want 3.543 Health but 3 or 4
         slider.$$anonymous$$Value = 0f;
         slider.maxValue = $$anonymous$$axHealth;
         slider.value = $$anonymous$$axHealth;        // start with full health
     }
 
     private void Update() {
         UpdateHealthBar(counter);        // just for testing purposes
         counter--;                        // just for testing purposes
     }
 
     public void UpdateHealthBar(int val) {
         slider.value = val;
         Fill.color = Color.Lerp($$anonymous$$inHealthColor, $$anonymous$$axHealthColor, (float)val / $$anonymous$$axHealth);
     }
 }
avatar image mayu_polo · Dec 24, 2014 at 01:07 PM 0
Share

Thanks a lot! Will try this. I didn't realize you could assign child objects like the Fill to a public variable.

One question though. I though the slider value only goes between 0 and 1 but you are assigning 500 to the value. Can you assign any max and $$anonymous$$ values to the slider?

Thanks!

avatar image Mexallon · Dec 24, 2014 at 03:32 PM 0
Share

Yes. See the Start() $$anonymous$$ethod of my Script (Lines 18 & 19). Its a public variable of the Component named Slider. It's much easier because you dont have to normalize your Health to the intervall [0, 1] that way.

avatar image Mexallon · Dec 27, 2014 at 03:52 PM 0
Share

Did it work?

avatar image
0

Answer by mazo0 · Nov 03, 2019 at 06:54 PM

This is how I could do that

 public Slider slider; 
 ...
 Color color = new Color(233f/255f, 79f/255f, 55f/255f);
 slider.gameObject.transform.Find("Fill Area").Find("Fill").GetComponent<Image>().color = color; 






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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

GUI.RepeatButton funcionality with new Unity UI (4.6.x) 2 Answers

Fall down a line in Unity 4.6 text via script 3 Answers

Particle System In 4.6 UI 6 Answers

How to convert old GUI to new 4.6 UI? 1 Answer

Push a UI-Element away with another 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