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 Oatz · Aug 27, 2014 at 01:10 PM · guitexturetoggletoggle button

Toggle between GUITextures

So here's my problem, I have two buttons, and i need one to be highlighted while the other is not, simple as that. If i select a non highlighted button it will be set active and highlight while setting the other button non active and thus not highlighted.

For the life of me i cannot get the colors to change. It will set the bool to true or false but in the colorChanger method the color wont change and the debug wont show.. I'm certain its my lack of programming experience.

I'm terrible at c# or any programming language so bear with me..

Here is what I have.

Color changing script- attached to both buttons.

     public bool colorChange = false;
     public Color buttonColor;
     public Color activeButtonColor;
     
     void start{
     }
     
     void OnMouseDown () {
     colorChange = true;
     
     //This changes opposite buttons "colorChange" bool to false..
     GameObject object1 = GameObject.Find("mygameobject");
     object1.GetComponent<mygameobjectScript>().colorChange = false;
     }
     
     void colorchanger () {
     if(colorChange){
     guiTexture.color = activeButtonColor;
     Debug.Log ("color change true");
     }
     
     else{
     guiTexture.color = buttonColor;
     }
     }
     }

I tried to make it clear sorry if this is utter crap I'm new to this. Thanks for any help.

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
0

Answer by robertbu · Aug 27, 2014 at 03:02 PM

I suspect your problem is with GameObject.Find(), but rather than work though how you have things structured, I'm going to suggest an alternate solution. 'static' variables are shared by a class. If you read enough posts on UA, you will find dire warnings about using static variables, but I find they are 'safe' and effective communicating between instances of a class. So here is a rewrite of your code:

 using System.Collections;
 using UnityEngine;
 
 public class Example : MonoBehaviour {
 
     private static Transform active = null;
     public Color buttonColor = Color.white;
     public Color activeButtonColor = Color.red;
 
     void Start() {
         guiTexture.color = buttonColor;
     }
     
     void Update() {
         if (Input.GetMouseButton (0)) {
             if (active != transform) {
                 guiTexture.color = buttonColor;
             }
         }
     }
     
     void OnMouseDown () {
         active = transform;
         guiTexture.color = activeButtonColor;
     }
 }

This rewrite will work with any number of GUITextures with this script.

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 Oatz · Sep 03, 2014 at 05:48 AM

Figured it out. Thanks for trying though I don't think I was very clear.

Here is what I came up with for toggling between two buttons.. With the script attached to each button, the script will change the bool "colorChange" on the opposite button.

 public bool colorChange =  false;
 public Color buttonColor;
 public Color activeButtonColor;
         
 
     
     void Update(){
 
     if (colorChange == true){
             guiTexture.color = activeButtonColor;
         }
     else{
             guiTexture.color = buttonColor;
         }
 
     }
 
 
 
      void OnMouseDown (){
         colorChange = true;
         if(colorChange){
             GameObject buttonOne = GameObject.Find("Button One");
             buttonOne.GetComponent<ButtonOneScript>().colorChange = false;
         
 
         
             }
             
         
 
 
 
         }
         
         
         
         
 

}

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

2 People are following this question.

avatar image avatar image

Related Questions

Toggle Button Question 0 Answers

spawn and destroy with toggle button? 1 Answer

Toggle Group event called 2 times Unity UI 1 Answer

Toggle full screen double click issue 2 Answers

Reduce Draw call for Multiple GUI Textures with same Texture 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