Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tabu · Dec 06, 2012 at 09:35 PM · onguiifgui.skin

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image prototype7 · Dec 07, 2012 at 12:08 AM 1
Share

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);
                  }
                }
             }
avatar image Tabu · Dec 07, 2012 at 07:19 AM 0
Share

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)

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tabu · Dec 07, 2012 at 07:26 AM 0
Share

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 :)

avatar image Bunny83 · Dec 07, 2012 at 01:11 PM 1
Share

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?

avatar image Tabu · Dec 09, 2012 at 12:09 PM 0
Share

Thank you :) And sorry for the trouble :( As decribed below, I found out what the problem was. Errorcode 40, unfortunately.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

If Statement within OnGUI not working - any ideas? 4 Answers

Encaplsue function in if statement 1 Answer

Drawing a GUI box by specifying the two corners 1 Answer

Placing a chat bubble above a character's head in a 3d environment 2 Answers

Saving a button in PlayerPrefs 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges