- Home /
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.
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)?
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.
Your answer
Follow this Question
Related Questions
Simple OnGui does not work 1 Answer
GUILayout Context Menu on Right Click - Not Working! 1 Answer
Distribute terrain in zones 3 Answers
How do i make a menu close if i open another menu 1 Answer
Multiple Cars not working 1 Answer