- Home /
Not working gui buttons
I have tried in both Unity 3 and Unity 4, but neither will display the GUI buttons. I am following the remixgames FPS tutorial, for anyone wondering. Thanks.
This is the code:
using UnityEngine;
using System.Collections;
public class Menu : MonoBehaviour {
private string CurMenu;
public string Name;
void Start () {
CurMenu = "Main";
}
void Update () {
}
void ToMenu (string menu) {
CurMenu = menu;
}
void OnGUI () {
if(CurMenu == "Main")
Main();
if(CurMenu == "Host")
Host();
}
private void Main () {
if(GUI.Button(new Rect(0,0,128,32),"Host a match")) {
ToMenu("Host");
}
Name = GUI.TextField(new Rect(130,0,128,32),Name);
if(GUI.Button(new Rect(260,0,128,32),"Save")) {
PlayerPrefs.SetString("PlayerName", Name);
}
}
private void Host () {
}
}
Comment
Answer by robertbu · Apr 15, 2014 at 04:36 PM
Subtle problem. You need to change your function name to something other than 'Main'. I have a guess why, but I'm not completely sure of the reasons. Anyway your code started working for me when I changed the name.
Not sure what you are asking. To put the script in the scene, create an empty game object and drag and drop this script on the game object. The script worked fine for me once I change the name of '$$anonymous$$ain()'.