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 Elainduel · Dec 05, 2014 at 01:50 PM · c#trigger

Trigger GUI with a key when colliding (C#)?

Hey all,

I'm completely new to scripting and Unity, and I have to finish a Unity project as part of a module in my Master's course. The logic of scripting still seems to escape me, I can execute all sorts of simple commands but when it comes to combining many different commands into one script I'm lost. Can anyone help me with the following scenario? It would have to be in C#; I've seen some solutions to this in Java but our projects need to be in C, and I just don't have the skills to translate from one language to another. Also I know the new UIs have come with the new update and that they're more intuitive to use, but we aren't allowed to use them either, I'm using Unity 4.3 atm.

My game is a simple educational explore-an-environment-and-click-on-things-to-learn-about-them. I have multiple different objects to explore, all with different GUIs. I want to have

1) a GameObject in my scene that when the player collides with it, a question mark GUI appears to let them now it's possible to investigate the object,

2) and then when they press key X when on this collider area a GUI window with text pops up,

3) when they press X again the window disappears,

4) When they leave the collider area the question mark GUI disappears.

I can make the texture appear and disappear when the player collides with the trigger, I can toggle GUI windows, I can trigger events with the pressing of a key - but I can't get my head around how to combine these things, as in, what needs to be void onTriggerEnter, what on void Update, what on onGui etc. etc... How to make the condition that this specific GUI will appear ONLY if the player is on this specific area and ONLY if she presses X? Any help would be greatly appreciated!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Baste · Dec 05, 2014 at 02:26 PM

You'll need to use some bools to set the state of your script. Something like:

 bool playerInTrigger;
 bool showWindow;

 void OnTriggerEnter(Collider c) {
     if(//check if the Collider c belongs to the player here. If only the player moves, skip the if) {
         playerInTrigger = true;
     }
 }

 void OnTriggerExit(Collider c) {
     if(//same check as in OnTriggerEnter) {
         playerInTrigger = false;
         showWindow = false;
     }
 }

 void Update() {
     if(playerInTrigger && Input.GetKeyDown(KeyCode.X)) {
         showWindow = !showWindow;
     }
 }

 void OnGUI() {
     if(showWindow) {
         //Draw the window here
     }
 }

This is pretty basic stuff. I'd look into some more tutorials if I were you! But the skeleton above should get you started; you can only open the window if the player is in the trigger, and if you leave the trigger area, the window closes.

About where to put what - OnTriggerEnter fires when you enter the trigger, and only then. OnGUI and Update fires once every frame. If you put GUI code in Update, Unity complains and refuses to draw your GUI. You could theoretically put input code in OnGUI, but oh God why would you it's called OnGUI.

Rant:

I will have to keep on saying this over and over again to my dying day, but Java and Javascript are not the same thing. Not even remotely close. When you're saying:

"It would have to be in C#; I've seen some solutions to this in Java but our projects need to be in C"

There's no Java in the Unity world - only a variant of Javascript. They're NOT THE SAME - Java is a lot more similar to C# than it is to Javascript. Also, C and C# are about as different as elephants and tax reform, so your project definitely does not need to be in C.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Call StartCoroutine() from inside a function other than Start!? 2 Answers

how can i auto target Gameobject on startup 1 Answer

Animation triggered by an animation? 1 Answer

How To Make An Invisible Wall That's Impossible To Get Out Of? 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