[Help][Noob] My Enums/Input won't work
I just bought the learn c# programming with Unity3D udemy course and I've already gotten stuck. My text on screen won't change. It just goes to the first enum thing in the list and it just sets it to that. I have no clue what I'm doing wrong, please help!!! I've tried multiple different keycodes and I've gone through the videos multiple times. Please help :/
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class textManipulator : MonoBehaviour {
public Text text;
private enum States{off, on};
private States myState;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(myState == States.on){
state_on ();
}
else if(myState==States.off){
state_off();
}
}
void state_on(){
text.text="Your state if currently on!";
if(Input.GetKeyDown(KeyCode.S)){
myState=States.on;
}
}
void state_off(){
text.text="Your state is currently off!";
if(Input.GetKeyUp(KeyCode.S)){
myState = States.off;
}
}
}
What did I do wrong? Why is it just setting the state to the first enum in the list?
Answer by saschandroid · Jun 28, 2016 at 08:33 AM
Because you never change myState
with your code. States myState
sets it to the first one in the enum. If myState == States.on
you call state_on()
and if you press 'S' you set it to States.on
again (so you don't change it). I guess you want myState = States.off
in state_on()
and myState = States.on
in state_off()
(or call state_off() if myStates == States.on and state_on if myStates == States.off):
void state_on(){
text.text="Your state if currently on!";
if(Input.GetKeyDown(KeyCode.S)){
myState=States.off;
}
}
void state_off(){
text.text="Your state is currently off!";
if(Input.GetKeyUp(KeyCode.S)){
myState = States.on;
}