Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ObviouslyInsane · Aug 14, 2011 at 12:13 AM · guihideshow

Show GUI on collision

Hi everyone, I have a simple script here I'm having a small problem with:

 var showGUI = false;
 var launcherUpgrade1 : Texture2D;
 
 
 function OnTriggerEnter (col : Collider) {
         
 
 if(col.name == "Player" && GUI.enabled == false) {
 GUI.enabled = true;
 }
 
 if(GUI.enabled == true) {
 OnGUI();
 }
 
 else if(GUI.enabled == false) {
 
 return;
 }
 }
 
 
 function OnGUI () {
    GUI.Box (Rect (10,10,100,50), GUIContent("400", launcherUpgrade1));
 }

So basically when the player collides with the gameobject this script is attached to I want it to show the GUI. However, the GUI is automatically being shown as soon as the game starts. How can I fix this?

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

2 Replies

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

Answer by Waz · Aug 14, 2011 at 12:24 AM

You misunderstand GUI.enabled. It's not for that. It's for "greying out" UI elements. Try this:

 var showGUI = false;
 var launcherUpgrade1 : Texture2D;
 
 function OnTriggerEnter (col : Collider) {
     if(col.name == "Player")
         showGUI  = true;
 }
 
 function OnGUI () {
    if (showGUI) {
        GUI.Box (Rect (10,10,100,50), GUIContent("400", launcherUpgrade1));
        ... rest of the GUI ...
    }
 }

Or, if you really do want it to grey out:

 function OnGUI () {
    GUI.enabled = showGUI;
    GUI.Box (Rect (10,10,100,50), GUIContent("400", launcherUpgrade1));
 }
Comment
Add comment · Show 2 · 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 ObviouslyInsane · Aug 14, 2011 at 12:34 AM 0
Share

Aha, now it makes sense. Thank you for clearing that up for me, it works perfectly now.

avatar image Dune · Apr 17, 2013 at 01:39 AM 0
Share

This was incredibly helpful. Thank you so much.

avatar image
5

Answer by Ziad · Aug 14, 2011 at 12:27 AM

make a script and name it

GUIControllerScript.cs

 using UnityEngine;

using System.Collections;

public class GUIControllerScript : MonoBehaviour {

 public bool ShowGUI = false;

 void Start () 
 
 {
 
 }
 
 void Update () 
 {
     if (ShowGUI)
     {
         G_U_I.Instance.enabled = true;
     }
     if (!ShowGUI)
     {
         G_U_I.Instance.enabled = false;
     }
 }

 void OnTriggerEnter(Collider col)
 {
     if (col.name == "Player")
     {
         ShowGUI = true;
     }
 }


}

and this one name it

G_U_I.cs

 using UnityEngine;

using System.Collections;

public class G_U_I : MonoBehaviour { public static G_U_I Instance;

 void Start () 
 {
     Instance = this;
 }
 
 void Update () 
 {
 
 }


 void OnGUI()
 {
     //Put ur GUI Script here
 }

}

Comment
Add comment · Show 1 · 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 Waz · Aug 14, 2011 at 02:13 AM 0
Share

Conceptually, this approach has the advantage that the OnGUI call is avoided by disabling the object completely. Your actual implementation though does that at the expense of having another Update() every frame, which negates that benefit. Conceptually though, it is correct.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

hide button 1 Answer

How to hide/unhide a GUI image when character enters trigger. 1 Answer

show gui when look certain object 2 Answers

Confusing Logic (Just need some help with what goes where) 0 Answers

NGUI: Recommended way of hiding/showing multiple menu panels? 1 Answer


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