- Home /
GUI.skin not working inside if
Hi, I have a problem that is hard for me to understand? When I make the following in my code, Unity is not able to use my custom skin (It simply does not use guiskin)
void OnGUI ()
{
if (showRestartButton) {
GUI.skin = guiSkin;
if (GUI.Button (new Rect (Screen.width * .5f - 380, Screen.height * .5f + 150, 350, 90), " ", "Restart")){
Application.LoadLevel (Application.loadedLevel);
}
}
}
However, if I remove if(showRestartButton) everything starts working as intended?
void OnGUI ()
{
GUI.skin = guiSkin;
if (GUI.Button (new Rect (Screen.width * .5f - 380, Screen.height * .5f + 150, 350, 90), " ", "Restart")){
Application.LoadLevel (Application.loadedLevel);
}
}
Can anyone explain this to me? It confuses me quite a bit.
I think you should declare which GUI to use to each button for example
void OnGUI ()
{
// it will not use custom skin if you don't mention it
GUI.skin = guiSkin;
if (showRestartButton) {
//as long as you don't the change the skin it will use your custom skin for the rest of button
//GUI.skin = guiSkin;
if (GUI.Button (new Rect (Screen.width * .5f - 380, Screen.height * .5f + 150, 350, 90), " ", "Restart")){
Application.LoadLevel (Application.loadedLevel);
}
}
}
Hey, and thank you for taking time to answer :) This is how I initially set it up, because that is how I understand it as well.. which is why I get confused.
When I set it up like you suggest I get the Warn message: (I get this warn message whenever I have the if (showRestartButton) is in my OnGUI() )
Unable to find style 'Restart' in skin 'GameSkin' Layout UnityEngine.GUIStyle:op_Implicit(String) WinTrigger:OnGUI() (at Assets/Scripts/WinTrigger.cs:49)
Answer by Tabu · Dec 07, 2012 at 07:40 AM
Never mind... I did find the problem! Somehow, someone on the team added this script to several instances in the level. The other instances did not have the GUISkin attached... So show gui from several instances is not a good idea, I am moving it down to one instance only :) Sorry for the inconvenience :( but thank you so much for your help.
Answer by Bunny83 · Dec 07, 2012 at 12:43 AM
I don't see this behaviour. I used the GUISkin alot and assigned them also in an if statement. That wouldn't make much sense if that changes something. GUI.skin is a state. It doesn't change during the execution of OnGUI unless you change it. Usually you have one skin and assign it just at the beginning of OnGUI.
Anyway, your two examples don'z make a difference (except that the button might not be shown in your first example when showRestartButton is false)
Hi Bunny Thanks for your answer :) It does not make much sense to me either?
But'z, there is a difference ;) In one case it works, and in the other it does not. It would be nice if I could talk you into trying this out yourself with a boolean deciding if GUI is shown or not? This will help me to find out if there is something wrong with the way I set it up :)
I have such a setup for my debug class which is by far more complex. It has dynamic generated windows which can be shown and closed. I set the gui skin right before i draw it within a conditional statement.
From the error you posted it's seems that your skin doesn't have the custom style "Restart", are you sure that this style exists in your skin?
I got the feeling that you actually didn't want to use a different style... The text on your button is empty,
Take a look at the documentation, you use the 4th version of GUI.Button. Are you sure your parameters are correct that way?
Thank you :) And sorry for the trouble :( As decribed below, I found out what the problem was. Errorcode 40, unfortunately.