# Why I cant change the GUILayout.Button color to green when click on a button?

I used a break point and it's getting to the line:

style.normal.textColor = Color.green;


But not changing the color of the clicked button. And there are no any errors or exceptions.

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using UnityEditor;
using UnityEngine;
using System.IO;

public class Manager : EditorWindow
{
static void Manage()
{
EditorWindow.GetWindow<Manager>();
}

private void OnGUI()
{
var style = new GUIStyle(GUI.skin.button);
style.normal.textColor = Color.red;
style.fontSize = 18;

string[] assetPaths = new string[2];
string[0] = "Test";
string[1] = "Test1";

foreach (string assetPath in assetPaths)
{
if (assetPath.Contains(".test")
{
var i = assetPath.LastIndexOf("/");
var t = assetPath.Substring(i + 1);

if (GUILayout.Button(t, style, GUILayout.Width(1000), GUILayout.Height(50)))
{
style.normal.textColor = Color.green;
}
}
}
}
}

• OnGUI runs every frame move the initial style set up outside of onGUI create an init function and call it at the end of the Manage method, it's being reset to red every frame, I think. – Pheonix2105 Aug 7 '18 at 22:34

Edit : Removed an incorrect answer updated with hopefully a correct one.

It turns the solution is pretty simple

    style = new GUIStyle(EditorStyles.toolbarButton);
//the color if the button is pressed
style.active.textColor = Color.red;
//the default color
style.normal.textColor = Color.green;

style.fontSize = 18;


The full code

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using UnityEditor;
using UnityEngine;
using System.IO;

public class Manager : EditorWindow
{
static void Manage()
{
EditorWindow.GetWindow<Manager>();
}

private void OnGUI()
{
var style = new GUIStyle(GUI.skin.button);
style.normal.textColor = Color.red;
style.active.textColor = Color.green;
style.fontSize = 18;

string[] assetPaths = new string[2];
string[0] = "Test";
string[1] = "Test1";

foreach (string assetPath in assetPaths)
{
if (assetPath.Contains(".test")
{
var i = assetPath.LastIndexOf("/");
var t = assetPath.Substring(i + 1);

if (GUILayout.Button(t, style, GUILayout.Width(1000), GUILayout.Height(50)))
{
}
}
}
}
}


Also worth noting but not related to answering your question is EditorStyles is full of useful Unity styles you can apply to elements.

• I'm getting some exceptions the first one is : ArgumentException: You can only call GUI functions from inside OnGUI that on the line that call Initi(); – Benzi Avrumi Aug 7 '18 at 22:43
• GUIStyle itself isnt a GUI function like GUI.Button etc so that's strange, I'll set up a demo and see if I can reproduce the error and get back to you. – Pheonix2105 Aug 7 '18 at 22:45
• Sorry there was an extra bracket in the code above and I was wrong about the GUIStyle, in my code it also only runs inside OnGUI just each object of mine calls its own init function once inside. I'll update the post with the way I've used to change colors on certain actions. – Pheonix2105 Aug 7 '18 at 23:04