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 Ufando · Aug 20, 2012 at 02:18 AM · colorguitexturechange

How to change a guiTexture color when a float is a certain number?

If my question was a bit vague i'll expand here;

Basically what i want to do is have a little icon of a stomach (To represent hunger) and every frame have a variable that contains the hunger decrease by 0.1. Once the hunger level reaches certain intervals, i want the color to change on the guiTexure, for example; at 500.0f color = yellow at 250.0f color = red

I'm fairly new to scripting in unity, so i could be way off with this code, so can somebody please help me

It will decrease the CurrentHunger variable, however the colors do not change at the intervals.

Here's my code;

 using UnityEngine;
 using System.Collections;
 
 public class Hunger : MonoBehaviour 
 {
     public float CurrentHunger = 1000.0f;
     public float MaxHunger = 1000.0f;
     // Use this for initialization
     void Start () 
     {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         CurrentHunger -= 0.1f;
 
     if (CurrentHunger > MaxHunger)
         {
             CurrentHunger = MaxHunger;
         }
     }
     void OnGui ()
     {
         if (CurrentHunger == 500.0f)
         {
             guiTexture.color =  Color.yellow;
         }
     else if (CurrentHunger == 250.0f)
         {
             guiTexture.color =  Color.red;
         }
     }
 }
 
Comment
Add comment · Show 1
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 Khada · Aug 20, 2012 at 05:05 PM 0
Share

Did that do the trick? Don't forget to mark the question as answered if it did.

1 Reply

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

Answer by Khada · Aug 20, 2012 at 07:29 AM

A line like this:

 if (CurrentHunger == 500.0f)

is actually testing whether or not x.xxxxxxxxx is exactly equal to 500.0000000 which is a bad way to do things. Even if your logic makes it clear that the if statement should trigger at some point, floating point numbers are not 100% accurate, and so still may not end up exactly the value you expect.

instead, use less-than and greater-than checks:

 void OnGui ()
 {
     if (CurrentHunger < 250.0f)
     {
         guiTexture.color =  Color.red;
     }
     else if (CurrentHunger < 500.0f)
     {
         guiTexture.color =  Color.yellow;
     }
 }
Comment
Add comment · Show 2 · 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 Ufando · Aug 20, 2012 at 09:18 PM 0
Share

Yup that worked, i also needed to call the OnGui function in update and that got it working, thanks.

avatar image Khada · Aug 21, 2012 at 04:36 AM 0
Share

Oh, you shouldn't need to do that. You actually just need to rename the function from "OnGui" to "OnGUI".

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

10 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

Related Questions

changing the color of particles using the legacy particle animator? 2 Answers

Solid color flash entire screen 2 Answers

Disco like GUITexture? 1 Answer

Changing two different objects renderer colour 1 Answer

gui.label color change 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