GUI Style Issue-- Texture not changing

Hello,

My code is meant to iterate through a list and set all of the components to “blank.”

Blank is a blank inventory space and has its’ own GUIStyle associated with it.

I know that my code DOES set everything to blank, but I do not see any changes.

My code is as follows:

     IEnumerator SetItemList_Ienum(GUIComponent arrayParent)
    {
        yield return new WaitForEndOfFrame();

        for (int i = 0; i < arrayParent.numberOfChildElements; i++)
        {
            if (_currentItems.Count > 0)
            {
              arrayParent.childrenComponents <em>=  _currentItems*.GetComponent<GUIComponent>();*</em>

}
else
{
arrayParent.childrenComponents = Item_Special.instance.blankItemGUIComponent;
print(arrayParent.childrenComponents*.style.normal.background);*
}
}
}
The current count of the _currentItems list is 0, so it’s all happening in the else fragment.
Also, the print shows that the texture has indeed changed, but there is no visible difference.
The only thing I can think of is that I may have botched up calling apply. I was using this code:
void ApplyTextures()
{
if (_style.normal.background) // Apply the background Texture2D
{
_style.normal.background.Apply(); // Lag.
}
if (_style.hover.background) // Apply the hover Texture2D
{
_style.hover.background.Apply(); // Lag.
}
}
Anyone have any suggestions on how to fix this?

Calling Apply() on a texture will apply all previous SetPixel and SetPixels changes. What you need to do in order to change the background picture is just _style.normal.background = newBackgroundTexture. You can use them on any GUI or GUILayout components, by passing the name of the style as a string parameter. Refer to the API for more info. The changes take effect instantly.

Instead of changing a GUIStyle from code, I would instead create a new GUIStyle, and apply that when appropriate. In case you have a lot of GUIStyles, I hope you have read the documentation on GUISkins.

I did something like this:

using System.Collections;
using UnityEngine;

class MyInventory
{
	
	int maxInventorySpace = 0; // Set
	public GUISkin InvSkin;
	
	ItemType[] inv = new ItemType[maxInventorySpace];

	void OnGUI()
	{
		GUI.skin = InvSkin;
		
		int x = y = 0;
		int maxX; //Set
		int boxHeight; //Set
		int width, height; //Set
		
		foreach(ItemType i in inv)
		{
			
			if(x >= maxX)
			{
				x = 0;
				y = y + boxHeight;
			}
			GUI.Box(new Rect(x, y, width, height), i, "InvBox");
		}
	
	}
	
}

Pretty much just re-create your box over and over with a specific texture. Then you can add in conditionals and set the box style to another name.