Hey guys, i’m just recently learning game development and trying Unity. I have been trying to get something work right now, basically i just need a menu that can be controllable by up and down key. There are 3 menu which is “Play”, “Load”, and “Exit” which i create using textmesh, I’ve been trying with this code but somehow the color won’t stuck in the middle of the menu (Load Game). It keeps increasing the “choose” variable to two or zero. Maybe this is a simple logic error or something else, any help appreciated.
using UnityEngine;
using System.Collections;
public class MenuScript : MonoBehaviour {
int choose = 0; //0 -> Play 1-> load 2 ->exit
public GameObject playObj, loadObj, exitObj;
TextMesh playText, loadText, exitText;
// Use this for initialization
void Start () {
playText = playObj.GetComponent<TextMesh> ();
loadText = loadObj.GetComponent<TextMesh> ();
exitText = exitObj.GetComponent<TextMesh> ();
}
// Update is called once per frame
void Update () {
float v = Input.GetAxis ("Vertical");
if(v>0){ // up key
if(choose !=0)
choose--;
}
else if(v < 0){ // down key
if(choose !=2)
choose++;
}
//change color
if (choose == 0)
{
playText.color = new Color (255, 0 , 0);
loadText.color = new Color(0, 0, 0);
exitText.color = new Color(0,0,0);
}
else if (choose == 1)
{
playText.color = new Color (0, 0 , 0);
loadText.color = new Color(255, 0, 0);
exitText.color = new Color(0,0,0);
}
else if (choose == 2)
{
playText.color = new Color (0, 0 , 0);
loadText.color = new Color(0, 0, 0);
exitText.color = new Color(255,0,0);
}
}
}