Possible to access GUIStyle variables by code?


Is it possible to use code to change GUIStyle variables? For example, I would like to change the background image in a GUIStyle without making a new GUIStyle. It would go something like this:

GUI.Label(new Rect(0, 0, groupWidth + 10, screenHeight), "",
	sideBkgGUIStyle.Normal.Background = "some image");


Sure, you can set any member of a GUIStyle. However keep in mind that the change will be “permanent” for the current session. If the GUIStyle is a member of a GUISkin, changes at runtime will also be propagated into the GUISkin assets.

All things related to GUIStyles should be done inside OnGUI itself.

For example:

public Texture2d img1;   
public Texture2d img1;
private GUIStyle someStyle = null;
void OnGUI()
    if (someStyle == bull)
        //init the style for the first time
        someStyle = new GUIStyle("Button");
    if (someCondition)
        someStyle = img1;
        someStyle = img2;
    GUI.Label(new Rect(0, 0, groupWidth + 10, screenHeight), GUIContent.none, someStyle);

However in most cases it’s way easier to use a GUISkin with predefined custom styles. Take a look at my GUI crash course.