Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 animeman0 · Jul 11, 2015 at 04:25 PM · c#guibuttontriggerboolean

UI button doesn't appear - c#

Hi

In my game when the player falls of the edge and touches a trigger a gui buttom should appear however it doesn't.

 using UnityEngine;
 using System.Collections;
 
 public class GameOver : MonoBehaviour {
 
     public GameOverScript GameOverVar;
 
     public bool isClicked = false;
 
     bool guiOn = false;
 
     public float OnX = 15f;
     public float OnY = 15f;
     public float width = 100f;
     public float height = 50f;
     public string textInGUI = "Try again?";
 
     void OnTriggerEnter2D (Collider2D over)
     {
         if(over.gameObject.tag == "Player")
         {
             GameOverVar.isGameOver = true;
             if (GameOverVar.isGameOver == true)
             {
                 //Application.LoadLevel(1);
                 guiOn = !guiOn;
             }
         }
     }
     void OnGUI()
     {
         if(guiOn)
         {
             GUI.Button(new Rect(OnX,OnY,width,height), textInGUI);
             Debug.Log("It worked");
             guiOn = false;
         }
     }
 }

Also the Debug.Log("It worked"); does output in the console

Comment
Add comment
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

3 Replies

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

Answer by Rad-Coders · Jul 12, 2015 at 01:54 PM

The right code, and have been tested so it works.

 if(guiOn) 
     { 
     bool button = GUI.Button(new Rect(OnX,OnY,width,height),textInGUI); 
     if(button) 
     { 
     Debug.Log("It worked"); 
     guiOn = false; 
     } 
     } 



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
2

Answer by barbe63 · Jul 11, 2015 at 04:34 PM

By setting guiOn to false immediatly after displaying it you only display it for one frame. Just remove the guiOn = false or maybe put it like this:

      void OnGUI()
      {
          if(guiOn)
          {
              if(GUI.Button(new Rect(OnX,OnY,width,height), textInGUI))
              {
                  Debug.Log("It worked");
                  guiOn = false;
              }
          }
      }
Comment
Add comment · Show 12 · 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 animeman0 · Jul 11, 2015 at 05:15 PM 0
Share

thank you!

avatar image Rad-Coders · Jul 11, 2015 at 05:18 PM 0
Share

$$anonymous$$y plesuare

avatar image animeman0 · Jul 11, 2015 at 05:25 PM 0
Share

Oh wait it still doesn't work, nothing appears

avatar image Rad-Coders · Jul 11, 2015 at 05:44 PM 0
Share

Try this if(guiOn) { bool button = GUI.Button(new Rect(OnX,OnY,width,height), textInGUI); if(button) { Debug.Log("It worked"); guiOn = false; } }

avatar image barbe63 · Jul 11, 2015 at 05:54 PM 0
Share

Hmm that should work... weird! Paste your code please

Show more comments
avatar image
0

Answer by Rad-Coders · Jul 12, 2015 at 01:53 PM

The problem here is that once you make guiOn false it hides the button because the button will only show as long as guiOn is true. Try to make guiOn false once the player has clicked on the button you cando this all at once like this.

 if(guiOn)
 {
 if(GUI.Button(new Rect(OnX,OnY,width,height), textInGUI))
 {
 Debug.Log("It worked");
              guiOn = false;
 }
 }

This should work. And it creates the button so just replace that code with this one.

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

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

24 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to fire gui button on mouse right release 2 Answers

C# Boolean Doesn't Change Value 1 Answer

Trigger Sound more than Once 0 Answers

How to make a or array of GUI.Button's? 1 Answer

GUI.Button is acting funky. 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