- Home /
gui button custom skin not applying background
void OnGUI()
{
if (Input.GetKey(KeyCode.UpArrow))
{
GUI.skin = guiSkin;
float xpos = transform.position.x;
float ypos = transform.position.y;
Rect position = new Rect( xpos, 150 + ypos , 53 * 2, 87);
DrawQuad(position,Color.red);
GUI.backgroundColor = Color.clear;
GUI.SetNextControlName("1");
GUI.Button(new Rect(xpos, 150 + ypos, 53, 87), "", "button1");
if(Input.GetKey(KeyCode.RightArrow))
{
GUI.FocusControl("1");
//GUI.Button(new Rect(620 + xpos, 150 + ypos, 53, 87), guiSkin.focused.background);
//Debug.Log("true");
}
}
}
I have a custom gui skin that is hooked up through the inspector with an element named button1 that I am trying to display with the line
GUI.Button(new Rect(xpos, 150 + ypos, 53, 87), "", "button1");
but nothing shows up. However if I put "hi" or something as the text the hi shows up so it is being called button1 is just not showing up, I am receiving to errors or warnings so it "sees" button1 but it's just not showing.
edit: I have played around with colors on the text and I can confirm it's calling the button1 style but it's not displaying the background image, any help?
if button1 is a variable try removing the quotation
That errors, it's not a variable it's the name of the custom styles element. I have played around with text and defining colors in the style for normal, active, etc. The code works it's just not displaying the background image and it's really got me stumped.
Answer by chris200x9 · Jun 02, 2014 at 03:45 PM
Turns out this line was the culprit:
GUI.backgroundColor = Color.clear;
I was turning the background image clear.
Answer by TrickyHandz · Jun 02, 2014 at 12:43 PM
For the custom style to show properly without having a reference stored in a variable you need to change the button declaration to include a look up for the style. SO this line:
GUI.Button(new Rect(xpos, 150 + ypos, 53, 87), "", "button1");
Should look like this:
GUI.Button(new Rect(xpos, 150 + ypos, 53, 87), "", guiskin.FindStyle("button1"));
Just tried and both lines work exactly the same. Text works, changing colors according to the style with respect to normal, active, etc is fine. I really don't know what's wrong the code works in all respects except it does not show the background image.
Your answer
![](https://koobas.hobune.stream/wayback/20220613150537im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GUISkin or otherwise - Once clicked, button stays same color 1 Answer
Unable to find style in skin repaint 1 Answer
Whats wrong with my GUI.Toggle? 2 Answers
How to attach GUI Skin on button? 0 Answers
GUISkin is overlaying my button image 0 Answers