Changing text color of certain GUI.Button when clicked.

Hi, I am trying to have a GUI.Button’s text color change according to button that’s pressed.

In short I have a scrollView and the amount of buttons depends on a integer (i).

Here’ the code that displays the buttons, basically it’s creating a button for every testInteger.lenght (i).

If I click the third button, the value of i is 3. Ie, It’s already got an ID for each button.

Can I change the font color of the button that is pressed?

Cheers, Tim.

     for (var i:int = 0; i<testInteger.length; i++){
     if (GUI.Button(Rect (listX2 + (spcr),listY2 + (spacertop * i),listW2,listH2),"test")){

}

You can use a GUISkin or GUIStyle.

You can then define the color for normal, hover and on press.

public GUIStyle style;

for (var i:int = 0; i<testInteger.length; i++){
    if (GUI.Button(Rect (listX2 + (spcr),listY2 + (spacertop * i),listW2,listH2),"test", style)){
     
    }
}

The GUIStyle will show in your inspector and you can define the color. Note that you have to provide a background texture as well for normal/hover/active for the color to swap.

So either you create one or if you do not want any, just use a transparent texture.

Also, since you are not using the default unity GUI system, the frame of the button will not be there. SO without any back texture you end up with text only.

try this method.but for a group of buttons,its not easy to apply…Hope this helps.

//Define the rectangle for the first button
	Rect Rect_Button_01 = new Rect (10, 10, 120, 30);
	//Define the rectangle for the second button
	Rect Rect_Button_02=new Rect(10,50,120,30);
	//Define the color variable for the first button
	Color Button_Col_01=Color.white;
	//Define the color variable for he second button
	Color Button_Col_02 = Color.white;

	void OnGUI()
	{
		//Set the color for GUI
		GUI.color = Button_Col_01;
		//Draw the button
		GUI.Button(Rect_Button_01, "Hover To See");
		//Set back color to white
		GUI.color = Color.white;    

		//Set the color for GUI
		GUI.color = Button_Col_02;
		//Draw the second button
		GUI.Button (Rect_Button_02, "Click To See");
		//Set the color back to white
		GUI.color = Color.white;    

		//Convert the mouse point calculation from First Quadrant to Fourth quadrant
		Vector2 Mouseposition=new Vector2(Input.mousePosition.x,
		                                  Mathf.Abs(Screen.height-Input.mousePosition.y));

		//Set yellow if mouse is inside rectangle else set back to white
		if(Rect_Button_01.Contains(Mouseposition))
		{Button_Col_01=Color.yellow;}
		else
		{Button_Col_01=Color.white;}

		//Set red color if button is clicked else set color back to white
		if (Rect_Button_02.Contains (Mouseposition) && Input.GetMouseButton (0)) 
		{Button_Col_02 = Color.red;}
		else
		{Button_Col_02 = Color.white;}