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
1
Question by johnmm3316 · May 02, 2012 at 12:53 AM · c#guiinventorywindow

using c# script to open/close GUI window

Ive been tearing my hair out trying to solve this. Ok, lets admit the truth to the fact im a total rookie. This being said ill get to the point.

I am basically trying to get my inventory GUI window to open and close by pressing I.

My code looks like this :

 using UnityEngine;
 using System.Collections;
 
 public class Inventory : MonoBehaviour
 {
 
     void OnGUI()
     {
         // Make a background box
         GUI.Box(new Rect(10, 10, 100, 90), "Inventory");
     }
     
     private bool render = false;
     private Rect windowRect = new Rect (20, 20, 120, 50);
 
     public void ShowWindow() {
         render = true;
     }
 
     public void HideWindow() {
         render = false;
     }
 
     public void OnGUI() {
         if (Input.GetKey(KeyCode.I))
             ShowWindow();
 
               if (Input.GetKey(KeyCode.I))
             HideWindow();
 
         }
     }

 


doesnt work at all either lol. any help is greatly apprecited. Basically, I just am trying to have a GUI open up and close by pressing I. Thanks in advance for any help!

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
2

Answer by syclamoth · May 02, 2012 at 12:58 AM

What does 'render' do? From the looks of things, you're changing a variable that does nothing!

You have two 'OnGUI' functions declared. Don't do that. From the looks of things, the second one should be renamed 'Update' (since you're only doing things in there that should only be done in Update anyway), and then in your first OnGUI function try modifying it so that it actually checks the 'render' variable!

 void OnGUI()
 {
     if(render)
     {
         // Make a background box
         GUI.Box(new Rect(10, 10, 100, 90), "Inventory");
     }
 }

If you want the 'I' key to toggle the box, do this:

 void ToggleWindow()
 {
     render = !render;
 }

 void Update()
 {
     if(Input.GetKeyDown(KeyCode.I))
     {
         ToggleWindow();
     }
 }
Comment
Add comment · Show 9 · 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 johnmm3316 · May 02, 2012 at 01:04 AM 0
Share

What if I just did something like this?

void OnGUI() { if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.I)) { // $$anonymous$$ake a background box GUI.Box(new Rect(10, 10, 100, 90), "Inventory"); } }

ANd something to close the window?

avatar image syclamoth · May 02, 2012 at 01:06 AM 1
Share

Don't use Input.anything inside of OnGUI. It's generally a bad idea. In any case, that will display the inventory when the key is held down, which isn't what you want. Just do it my way- there's a reason why I'm answering the question, and you're asking it.

avatar image johnmm3316 · May 02, 2012 at 01:16 AM 0
Share

Well now it looks something like this :

{

 void OnGUI()
 {
     // $$anonymous$$ake a background box
     GUI.Box(new Rect(10, 10, 100, 90), "Inventory");


 }

 void ToggleWindow()
 {
     render = !render;
 }

 void Update()
 {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.I))
     {
         ToggleWindow();
     }
 }

}

but its still having problems. obvious, i need more than a hand, so im going to have to just learn from the complete basics ins$$anonymous$$d. But for the sake of I spent 5 hours trying to do this simple thing, what IS the solution?

avatar image syclamoth · May 02, 2012 at 01:18 AM 0
Share

Are you even reading my answer? You need to make the OnGUI function acutally check the 'render' variable. Look back at my original answer, it's right there, in the first code box.

$$anonymous$$eep in $$anonymous$$d that OnGUI has no persistent state. It is always evaluated in terms of the current call- you don't 'add' and 'remove' elements, you either draw them or you don't. If you want an object to stay on the screen for several frames, you need to specifically draw that object in every frame's OnGUI function. The easiest way to switch between different 'modes' in an OnGUI is to use an if-statement to check variables on the object (which is persistant) to deter$$anonymous$$e what should be drawn in that specific frame.

avatar image johnmm3316 · May 02, 2012 at 01:28 AM 0
Share

Yes im really trying here lol. I by no means am a guru, i openly admit i am a complete rookie and furthermore stated I am in over my head and need to go back to square 1. I know it seems simple to people who have been in it for a while. Not TRYING to sound ignorant, I just do :|

well my code looks like

{ void OnGUI() { if (render) { // $$anonymous$$ake a background box GUI.Box(new Rect(10, 10, 100, 90), "Inventory"); } }

 void ToggleWindow()
 {
     render = !render;
 }

 void Update()
 {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.I))
     {
         ToggleWindow();
     }
 }

}

i thought the if (render) was checking the render? But the problem is i get the errors :

Error 2 The name 'render' does not exist in the current context

for everything named render. I tried moving a few things around to mess with it, but it just made matters worse so i went back to this code. Like I said I think i am just trying to bite off quite a bit more than I can chew currently. Thats my biggest problem lol.

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Method is called, but GUI doesn't show up 1 Answer

Using an On-Screen GUI Button to show a GUI? 1 Answer

GUI not working correctly? (Double tooltip display)(c#) 1 Answer

How do I fix this null problem? 1 Answer

Distribute terrain in zones 3 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