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 Robomaster · Jun 17, 2012 at 12:19 PM · errorbuttoncolor

Button color change

WHat im trying to do is make it so that when i click on the button i want it to turn red, heres my code im not sure what wrong with it. Can anyone help me out with this. Thanks in advance.

using UnityEngine; using System.Collections;

public class GameTabs5 : MonoBehaviour {

 void OnGUI(){
     
     if (GUI.Button(new Rect(140,35,70,35), "Traps")){
     GUI.color = Color.red;
     }
 }

}

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
3
Best Answer

Answer by whydoidoit · Jun 17, 2012 at 12:24 PM

OnGUI is a bit hard to get the hang of to start with :) It is called every frame, so you just set a bool true when it has been clicked and use that on the subsequent frames:

 using UnityEngine; 
 using System.Collections;
 
 public class GameTabs5 : MonoBehaviour {
 
      bool buttonClicked = false;
 
 void OnGUI(){
 
      if(buttonClicked)
          GUI.color = Color.red;
 
     if (GUI.Button(new Rect(140,35,70,35), "Traps")){
         buttonClicked = true;
     }
  }
 }
Comment
Add comment · Show 10 · 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 Robomaster · Jun 17, 2012 at 01:17 PM 0
Share

Thanks it works really appreciate it, but when i click on 2 buttons there both red how do i make it so that the one that selected is red and when i click on another one that one lights up and the other one goes back normal.

Would this be the right code

GetComponent("GameTabs5"){ buttonClicked = false; }

avatar image whydoidoit · Jun 17, 2012 at 09:10 PM 0
Share

Sorry to be so long getting back to you (Sunday's always busy with the family). If you want two buttons which can both be red then this:

 using UnityEngine; 
 using System.Collections;
 
 public class GameTabs5 : $$anonymous$$onoBehaviour {
 
      bool buttonClicked = false;
      bool button2Clicked = false;
 
 void OnGUI(){
 
      if(buttonClicked)
          GUI.color = Color.red;
      else
          GUI.color = Color.white;
 
     if (GUI.Button(new Rect(140,35,70,35), "Traps")){
         buttonClicked = true;
     }

      if(button2Clicked)
          GUI.color = Color.red;
      else
          GUI.color = Color.white;
 
     if (GUI.Button(new Rect(140,35,70,35), "Traps")){
         button2Clicked = true;
     }


  }
 }

If you want the other button to always be normal just do a GUI.color = Color.white; after your if(GUI.Button) in the code above.

avatar image Robomaster · Jun 17, 2012 at 11:03 PM 0
Share

Yea Sundays are pretty busy for me to, but what i was asking was like for example i click the traps button it turns red, then i press the spells button i then want spells to turn red and then have traps go back to normal so that only one button is red at a time.

avatar image whydoidoit · Jun 17, 2012 at 11:05 PM 0
Share

This should achieve that effect:

 using UnityEngine; 
 using System.Collections;
 
 public class GameTabs5 : $$anonymous$$onoBehaviour {
 
      bool buttonClicked = false;
      bool button2Clicked = false;
 
 void OnGUI(){
 
      if(buttonClicked)
          GUI.color = Color.red;
      else
          GUI.color = Color.white;
 
     if (GUI.Button(new Rect(140,35,70,35), "Traps")){
         buttonClicked = true;
         button2Clicked = false;
     }
 
      if(button2Clicked)
          GUI.color = Color.red;
      else
          GUI.color = Color.white;
 
     if (GUI.Button(new Rect(140,35,70,35), "Traps")){
         button2Clicked = true;
         buttonClicked = false;
     }
 
 
  }
 }
avatar image whydoidoit · Jun 17, 2012 at 11:05 PM 0
Share

You could set one onf the buttonClicked variables to true if you want it on by default.

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Change the Colour of UGUI button in script? 2 Answers

Name of left ctrl? 1 Answer

Multiple Button Modifications 2 Answers

panel and button 1 Answer

guiTexture color half alpha White? 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