Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by The_Awesome_One · Sep 28, 2015 at 04:20 AM · uibuttoncolor

How do I get my Buttons to switch colors?

I'm working on a basic Match three in Unity using only the UI. For the objects to match I'm using the UI buttons and having the objects be different colors. For having the objects switch palaces I have a method that switches the color of the buttons. My difficulty is in calling the method upon pressing the buttons. using UnityEngine; using UnityEngine.UI; using System.Collections;

 public class match : MonoBehaviour {
     
     public float time = 30.0f;
     public float _score;
     public GameObject pausePanel;
     private Color[] colors = new Color[3];
     //public Button[] buttons;
     private Image[] image = new Image[16];
     public Image im1;
     public Image im2;
     public Image im3;
     public Image im4;
     public Image im5;
     public Image im6;
     public Image im7;
     public Image im8;
     public Image im9;
     public Image im10;
     public Image im11;
     public Image im12;
     public Image im13;
     public Image im14;
     public Image im15;
     public Image im16;
     public Image b;
     public Image c;
     public Image temp;
     
     void Start () {
         _score = 0.0f;
         colors [0] = Color.red;
         colors [1] = Color.blue;
         colors [2] = Color.green;
         image [0] = im1;
         image [1] = im2;
         image [2] = im3;
         image [3] = im4;
         image [4] = im5;
         image [5] = im6;
         image [6] = im7;
         image [7] = im8;
         image [8] = im9;
         image [9] = im10;
         image [10] = im11;
         image [11] = im12;
         image [12] = im13;
         image [13] = im14;
         image [14] = im15;
         image [15] = im16;
     }
 
     void Update () {
 
         time -= Time.deltaTime;
         
         if (time <= 0) {
             time = 0.0f;
         }
 
         if (time == 0 && _score == 0) {
             pausePanel.SetActive (true);
         }
         AssignColor ();    
     }
 
     void OnGUI()
     {
         GUI.Box (new Rect (10, 10, 30, 20), "" + time.ToString ("0"));
         GUI.Box (new Rect (85, 410, 30, 20), "" + _score.ToString ("0"));
     }
 
     void AssignColor()
     {
         foreach (Image i in image) {
             int colornum = Random.Range (0, colors.Length);
             if(i.color == Color.white){
                 i.color = colors[colornum];
             }
         }
 
     }
 
     public void getButton(Image i)
     {
         if (b == null) {
             b = i;
         } 
         else if (c == null) {
             c = i;
         }
 
         if (b != null && c != null) {
             Swap(b, c);
             b = null;
             c = null;
         }
     }
 
     void Swap(Image ac, Image bc)
     {
         //Image temp;
         temp.color = ac.color;
         ac.color = bc.color;
         bc.color = temp.color;
 
     }
 }

I have all my buttons in my Inspector able to activate getButton, but nothing happens when I press it. my Image b or Image c aren't taking them in.

Comment
Add comment · Show 2
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 The_Awesome_One · Sep 28, 2015 at 04:38 AM 0
Share

I managed to get the image b and c to take them in upon pressing them but they still won't switch colors.

avatar image amanpu · Sep 28, 2015 at 05:48 AM 0
Share

You are using AssignColor() in Update. Perhaps it is creating problem . Let me know if it helps

0 Replies

· Add your reply
  • Sort: 

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

34 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Change button color to intermediate value on onclick() event 1 Answer

UI text wont highlight 0 Answers

How do I change UI button background AND button text on click(pressed)? 0 Answers

How can I change the UI button color when clicked 1 Answer

How to do Image Swap with Text Color the with New UI Button Transition? 0 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