- Home /
How to change a color of a GUI Button on click
how do i change the color of GUI Button when clicked on thst particular button. can anyone help with C# code...
Thanks for your answer in Advance
Answer by fafase · May 09, 2012 at 12:42 PM
Ok I removed the old answer since I realized I was going nowhere. Now this one is tried and working:
bool blue;
void Start(){
blue =true;}
void OnGUI() {
if(blue)
GUI.color = Color.blue;
else
GUI.color = Color.yellow;
if(GUI.Button(new Rect(100,110,70,30), "A button"))
blue= !blue;
}
I did it with Js and translate it to c# here so you might want to check twice the syntax as I am not using c# and am not confident with it...
original Js version
var blue:boolean = true;
function OnGUI() {
if(blue)
GUI.color = Color.blue;
else
GUI.color = Color.yellow;
if(GUI.Button(Rect(100,110,70,30), "A button"))
blue= !blue;
}
Close with the C# :) ...
private boolean blue : private isn't necessary, it doesn't hurt anything, but private is default for C# ; and it's bool ->
bool blue = false ;
[you typo'd start with a lower-case 's' for void Start(){} :p ]
and if(GUI.Button(new Rect(....), "...")) [forgot the 'new']
Answer by Piflik · May 09, 2012 at 10:52 AM
You have to assign a GUIStyle to the button and replace the background image.
var gStyle : GUIStyle;
var blueTex : Texture;
var redTex : Texture;
function Start() {
gStyle.normal.background = blueTex
}
function OnGUI() {
if(GUI.Button(Rect(0, 0, 100, 100), "Change Color" gStyle))
gStyle.normal.background = redTex
}
hey my friend we can not directly apply texure to GUIStyle gStyle.normal.background = redTex it is giving an error
Your answer
![](https://koobas.hobune.stream/wayback/20220613071257im_/https://answers.unity.com/themes/thub/images/avi.jpg)