- Home /
Chaning button Color (uGUI)
Hello,
I want to change a button normal and highlighted colors via code (C#)in Unity 5 for use in Windows 8 / Phone 8.1. I followed the answer from Changing UI Button color C# -Solved , but when I build out project for Windows 8.1/ Windows phone I am getting error in Visual Studios 2013 5 CTP , but there are NO console errors and buttons are working correctly. The game plays fine in the editor.
Error from VS:
Warning 1
The command ""F:\Program Files\Unity\Editor\Data\PlaybackEngines\metrosupport\Tools\AssemblyConverter.exe" -platform=wp81 "G:\Unity Games\Outs\MathWin8\LTMathPhone8\\UnityEngine.dll" "G:\Unity Games\Outs\MathWin8\LTMathPhone8\\WinRTLegacy.dll" "G:\Unity Games\Outs\MathWin8\LTMathPhone8\\Assembly-CSharp.dll" "G:\Unity Games\Outs\MathWin8\LTMathPhone8\\UnityEngine.UI.dll"" exited with code 1. G:\Unity Games\Outs\MathWin8\LTMathPhone8\LTMathPhone8.csproj 190 5 LTMathPhone8
If I remove suggested fix from above, I get no errors. I don't think UnityEngine.UI.dll likes the above fix. Is there another way to set/change button colors?
Thanks,
John
Answer by John-Lisenby · Mar 08, 2015 at 07:50 PM
Thanks for the reply!
I got it to work by getting the component of the button and adding a new color.
Example: Bbutton.GetComponent().color = new Color(0f, 255f, 0f, 255f);
Thanks,
John
Answer by antx · Mar 08, 2015 at 03:54 PM
Yes you can access the color of a button via script. If you have a reference of your button, get the image component of it and change the color variable. http://docs.unity3d.com/ScriptReference/UI.Image.html
Buttons also have a colors variable which contains the colors of the different button states.
http://docs.unity3d.com/ScriptReference/UI.Button.html
http://docs.unity3d.com/ScriptReference/UI.Selectable-colors.html
http://docs.unity3d.com/ScriptReference/UI.ColorBlock.html
I hope this helps.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
Unity 4.6 - Interactable UI Elements 1 Answer