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 Baalhug · Dec 08, 2013 at 10:22 PM · buttonfunctiongui.window

Problem managing GUI:Window buttons

Hi, guys:

I'm having a problem with buttons inside a GUI Window. I want a message appears when the player clicks a button to remind him he has chosen that option, but i dont want to use option buttons, just normal ones. So i got this code:

 /*EDIT:*/ string txt_menu1="";
     void OnGUI (){
         GUI.Window(1, new Rect(20, 20, 180, 300), menu1, "Menu One");
     }
 
     void menu1(int windowID) {
     if (GUI.Button(new Rect(10, 20, 160, 50), "Choose option 1")) {
             UseType1=true;
             txt_menu1="You have chosen Option 1";
         }
         GUI.Label(new Rect(10,200,160,90), txt_menu1, "Box");
     }

This is not working. When i press the button nothing happens. I'm guessing the function menu1 is only called on creation though i've seen in the documentation some examples where the code is written there, in the Window creation function, same for the text to be shown (txt_menu1). What am i doing wrong?

Comment
Add comment · Show 4
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 ArkaneX · Dec 08, 2013 at 10:54 PM 0
Share

Are you sure you're not resetting txt_menu1 somewhere else in your code? If you copy this snippet into a new script, remove UseType1=true; line and declare txt_menu1 string variable outside of menu1 method, it works as expected.

avatar image Baalhug · Dec 09, 2013 at 12:05 AM 0
Share

So you can only run 1 line of code inside the function? If i declare txt_menu1 outside of menu1 method, how will it react to click? It will be created with that text ins$$anonymous$$d of when player clicks it. Right?

avatar image ArkaneX · Dec 09, 2013 at 08:07 AM 0
Share

You can use as many lines of code inside a function/method. I just suggested removing that line, because when you copy the snippet to a new script, UseType1 variable will not be declared there :)

And regarding txt_menu1 variable - it must be declared in a class, outside of the menu1 method, because this way it can keep its value after exiting the method.

avatar image Baalhug · Dec 09, 2013 at 04:19 PM 0
Share

txt_menu1 is declared in the body of that script, outside OnGUI and outside menu1 method, it belongs to the main class. You mean to change its value outside the method? But how can i tell the program to change its value when the player presses the button?

2 Replies

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

Answer by Statement · Dec 09, 2013 at 04:27 PM

Seems to work just fine here (4.3.1f1)

@ArkaneX is probably right in that you are setting the variables elsewhere.

(I also obtained a new ID from GUIUtility rather than passing it a magic number.)

 using UnityEngine;
 using System.Collections;
 
 public class NewBehaviourScript : MonoBehaviour
 {
     public bool UseType1;
     public string txt_menu1;
 
     void OnGUI ()
     {
         int id = GUIUtility.GetControlID(FocusType.Passive);
         GUI.Window(id, new Rect(20, 20, 180, 300), menu1, "Menu One");
     }
     
     void menu1 (int windowID)
     {
         if (GUI.Button(new Rect(10, 20, 160, 50), "Choose option 1"))
         {
             UseType1 = true;
             txt_menu1 = "You have chosen Option 1";
         }
         GUI.Label(new Rect(10, 200, 160, 90), txt_menu1, "Box");
     }
 }
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
0

Answer by Baalhug · Dec 09, 2013 at 04:59 PM

If i use GUIUtility it works. Gonna check that function because i have 2 windows (ids 1 and 2 mannually) and it's not allowing me running it twice.

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 Baalhug · Dec 09, 2013 at 05:03 PM 0
Share

No, it's good, i was copying the "int" declaration.

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

18 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

Related Questions

OnSelect and OnPointerEnter work in one script but not another on the same object 2 Answers

Button not executing function. C# 3 Answers

How do you call a function with a button? Unity 5 UI 8 Answers

The variable has not been assigned - but it has 1 Answer

Create GUI based on an array 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