- Home /
Question by
Mr. New Guy · Sep 01, 2014 at 11:04 PM ·
c#buttonenum
How to assign Enums to button?
I made a texttwist-like game and i cant put the enums that i declared to my buttons.
Here's my code:
using UnityEngine; using System.Collections;
public class JumbledGame : MonoBehaviour {
static T GetRandomEnum<T>()
{
System.Array A = System.Enum.GetValues(typeof(T));
T V = (T)A.GetValue(UnityEngine.Random.Range(0,A.Length));
return V;
}
public enum Menu {
Main,
Scores,
Game
}
enum Words {
adhoc,
irony,
crap,
life,
awe
};
public Menu currentWord ;
public Texture btnStart;
public Texture btnQuit;
public Texture btnScores;
public Texture lblTitle;
void OnGUI (){
GUILayout.BeginArea(new Rect(Screen.width /2 -350,Screen.height /2 -400, 750, 750));
GUILayout.BeginHorizontal ();
GUILayout.FlexibleSpace ();
if (!btnStart) {
Debug.LogError("Please assign a texture on the inspector");
return;
}
if (!btnQuit) {
Debug.LogError("Please assign a texture on the inspector");
return;
}
if (currentWord == Menu.Main) {
currentWord = Menu.Main;
GUILayout.Label(lblTitle);
GUILayout.Space(10);
GUILayout.BeginArea(new Rect(Screen.width /2 -460,Screen.height /2 -200, 500,500));
if(GUILayout.Button(btnStart, GUILayout.Width(250), GUILayout.Height(70))){
currentWord = Menu.Game;
}
if(GUILayout.Button(btnScores, GUILayout.Width(250), GUILayout.Height(70))){
currentWord = Menu.Scores;
}
if(GUILayout.Button (btnQuit, GUILayout.Width(250), GUILayout.Height(70))){
Application.LoadLevel("scene1");
}
GUILayout.EndArea();
}
if (currentWord == Menu.Game) {
currentWord = Menu.Game;
Words choice = GetRandomEnum<Words>();
GUILayout.Button("");
}
GUILayout.FlexibleSpace ();
GUILayout.EndHorizontal ();
GUILayout.EndArea ();
}
}
Comment