- Home /
How To make a Ui button Chance colours when pressed
I want a UI button to be able to chance between 3 different colours when it gets pressed/touched, I want it to loop between them forever. I have been looking all over internet for hours but can't find anything. I'am very new to both Unity and C#. Thanks for the help!
A good question, i did tests myself and got wired results that are not appropriate to answer the question.
Please do not post the same question twice. It wastes everyone's time. I answered and deleted the duplicate.
Put your colors in an array and let the button invoke some method that cycles an indexer with each button press.
Answer by bilalitani · Apr 27, 2015 at 04:58 PM
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class ColorChanger: MonoBehaviour
{
List<Color> colors = new List<Color> { Color.red, Color.blue, Color.green };
int colorIndex = 0;
// Returns next color in the list
Color GetNextColor()
{
// Get the color at colorIndex in the list
Color nextColor = colors[colorIndex];
// If we are less than the last index
if (colorIndex < colors.Count - 1)
{
// Add one
colorIndex++;
}
else
{
// Otherwise restart
colorIndex = 0;
}
// Return the chosen color
return nextColor;
}
// Attach this function to OnClick in the inspector
public void ChangeColor()
{
// Set the button's color to GetNextColor();
GetComponent<Button>().image.color = GetNextColor();
}
}
Thank you for the quick answer! but I got an error:
NullReferenceException: Object reference not set to an instance of an object ColorChange.ChangeColor () (at Assets/ColorChange.cs:39) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:110) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:574) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:716) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)
do you know what it is?
That's weird; I just tested it and it works just fine. However I did forget to mention that the ChangeColor function is supposed to be public so you can assign it in the inspector (just fixed that in the original code). After that, you should create a button, attach the ColorChanger script to it as a component, and then drag the Button gameobject into the OnClick event of the button and choose the ChangeColor function. Everything should work fine after that. The Inspector should look like this:
Thank you so much! It works now!
I have one more question for you if would be so kind to answer.
I have another button in my scene that changes to "scen02" and hat works fine , and then I have a button that takes me back to scen01.
The robe is that when I go back to scen01 from scen02 the color chancing button dosnt remember the color it was ons it goes back frome the beginning... is it a way to make the color chancing button to remember while you can jump back and forword between scen01 and scen02?
Thank you so much for the help Automatic!
Thank you Automatic,
but i got an error? you know how to solve it?
NullReferenceException: Object reference not set to an instance of an object ColorChange.ChangeColor () (at Assets/ColorChange.cs:39) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:110) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:574) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:716) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)
See this tutorial for more info about it not saving the color between scenes: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading
You'll need to tell the button somehow to remember either through playerprefs, saving data to a file, or just not destroying the button upon loading (if you wanted to hide it, you could just disable it). Good luck.
Answer by Calif96 · Apr 27, 2015 at 09:58 PM
If you press on one of your buttons, there are a variety of options. One of them is "Transition". You can change the mode from tint (tints the button to whatever colour you want), Sprite swap (allows you to use your images) and animation. Once in any of those modes,just change individual states below it. Good luck.