• 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
Question by Babilinski · Nov 06, 2011 at 10:02 PM · c#guimenuclick

C sharp menu problem with bottons

I am creating a GUI menu but i'm getting some problems.

heres my script

 using UnityEngine;
 using System.Collections;
 
 public class Menu : MonoBehaviour {
     
     private Rect windowRect;
     
     public float boxW;
     public float boxH;
     public float boxX;
     public float boxY;
     
     public float bottonW;
     public float bottonH;
     public float bottonX;
     public float bottonY;
     public float offset;
     
     public GUISkin mySkin;
     public Texture map;
     
     public float textureY;
     public float textureX;
     public float textureWidth;
     public float textureHeight;
     
     public bool on;
     public bool onMap;
     
     
     // Use this for initialization
     void Start () {
     boxW =Screen.width - 200f;
     boxH = Screen.height - 250f;
         
     boxX = (Screen.width / 2) - (boxW / 2);
     boxY = (Screen.height / 2) - (boxH / 2);
         
      bottonW = 196 ;
      bottonH = 33.7f;
      bottonX = boxX / 3 ;
      bottonY = 100;
      offset = 83;
         
     textureX = 239;
     textureY = 86;
     textureHeight = 141;
     textureWidth = 92;
     
     }
     
     void OnGUI() {
         GUI.skin = mySkin;
         
         if(on == true){
         
         windowRect = GUI.Window( 0, windowRect, MyWindow, "Menu");
         }
         
     
     }
     
     // Update is called once per frame
     public void Update() {
         windowRect    = new Rect(boxX, boxY, boxW,  boxH );
         
     }
     
     
     private void MyWindow( int id ) {
         Map();
         if(onMap == true){
         mapTexture();
         }
         Character();
         Sound();
         Graphic();
         Save();
         Exit();
 
 
     }
     
     private void Map() {
         
             if (GUI.Button(new Rect(bottonX, bottonY + (offset * 0), bottonW, bottonH), "Map"));
         
              Debug.Log("Clicked the button with text");
         
         //else 
         //onMap = false;
         
     }
     
     
     
     private void Character() {
         
             if (GUI.Button(new Rect(bottonX, bottonY + offset * 1, bottonW, bottonH), "Character"));
     }
     
     private void Sound() {
         
             if (GUI.Button(new Rect(bottonX, bottonY + offset * 2, bottonW, bottonH), "Sound"));
     }
     
     private void Graphic() {
         
             if (GUI.Button(new Rect(bottonX, bottonY + offset * 3, bottonW, bottonH), "Graphics"));
     }
     
     private void Save() {
         
             if (GUI.Button(new Rect(bottonX, bottonY + offset * 4, bottonW, bottonH), "Save"));
     }
     
     private void Exit() {
         
             if (GUI.Button(new Rect(bottonX, bottonY + offset * 5, bottonW, bottonH), "Exit"));
     }
     
     
     private void mapTexture(){
         
         GUI.DrawTexture(new Rect(textureX, textureY, boxW  - bottonW - textureWidth, boxH - textureHeight), map, ScaleMode.StretchToFill, true, 10.0F);
         
         
             
             
         
     }
 }
         
     
     
         


 





The problem that I am having is that when I turn on my menu and its bool is set to true ; my botton also gets pressed.

Comment

People who like this

0 Show 1
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 syclamoth · Nov 06, 2011 at 11:57 PM 0
Share

What's the problem here? I don't understand what you are asking! Do you not want the button to be pressed? Is opening the menu pressing the button for you? Do you want your buttons to actually do something (which they currently do not)?

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by syclamoth · Nov 07, 2011 at 12:00 AM

Problem is that this line here

 if (GUI.Button(new Rect(bottonX, bottonY + (offset * 0), bottonW, bottonH), "Map"));

     Debug.Log("Clicked the button with text");

Doesn't do what I think you think it does. You have a semicolon at the end of the if statement, which means it doesn't act the way it normally would, and always executes the line after it no matter what the boolean in the middle says. You should change it to this, to be safer-

 if (GUI.Button(new Rect(bottonX, bottonY + (offset * 0), bottonW, bottonH), "Map"))
 {
     Debug.Log("Clicked the button with text");
 }

This is true of all your other buttons- I see all the rest are just a single if-statement, with a semicolon at the end! If you are doing that, you may as well just call

 GUI.Button(new Rect(bottonX, bottonY + offset * 4, bottonW, bottonH), "Save");

Because it does the exact same thing.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Once I paused my game how do I bring up a menu? 1 Answer

Passing values between scripts 5 Answers

GUI Problem 2 Answers

When I pause my game and enable canvas and then resume my keyboard starts controlling the menu... 1 Answer

Horizontal Slider with Labels 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