Hi:
I’m developing my own text adventure game . In part of my game, I’m going to offer 5 different options for the player to choose, those options will lead the player to 3 states: WIN, WRONG or LOST. Only one option(right option) will lead the player to the WIN state, the other 4 options will lead to WRONG state. The player will only have 3 chances to pick the right option to win. If the wrong options was selected over 3 times, the player will be led to the LOST state.
My question is how can i realize that if the player choose wrong option over 3 times, the state will lead to the LOST state but not the WRONG state. I guess it is about recording and reading the number of times that the WRONG state method being called, but i don’t know how to do.
I use google searched for an answer, and tried to follow the example “enter the wrong password 3 times will lock the account”, but it doesn’t work. Here is my code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour {
public Text text;
private enum States {StartState,Options, Wrongoption,WrongOption3,CoretOption }
private States mystate;
// Use this for initialization
void Start () {
mystate = States.StartState;
}
// Update is called once per frame
void Update () {
if (mystate == States.StartState) { StartState(); }
if (mystate == States.Options) { Options(); }
if (mystate == States.Wrongoption) { Wrongoption(); }
if (mystate == States.WrongOption3) { WrongOption3(); }
if (mystate == States.CoretOption) { CoretOption(); }
}
void StartState()
{
text.text = "Let's begin";
if (Input.GetKeyDown(KeyCode.Space)) { mystate = States.Options; }
}
static int x = 0;
void Options()
{
text.text = "Please select the right option, you have three chances: \n" +
" 1. 123\n" +
" 2. 456\n" +
" 3. 789\n" +
" 4. 468\n" +
" 5. 165\n" +
"Please use keypad number to choose the correct password option";
if (Input.GetKeyDown(KeyCode.Keypad1)) { mystate = States.Wrongoption; }
if (Input.GetKeyDown(KeyCode.Keypad2)) { mystate = States.Wrongoption; }
if (Input.GetKeyDown(KeyCode.Keypad3)) { mystate = States.Wrongoption; }
if (Input.GetKeyDown(KeyCode.Keypad4)) { mystate = States.CoretOption; }
if (Input.GetKeyDown(KeyCode.Keypad5)) { mystate = States.Wrongoption; }
if (x >= 3) { mystate = States.WrongOption3; }
}
int Wrongoption() {
text.text = "Wrong Option";
"Press R to return";
if (Input.GetKeyDown(KeyCode.R)) { mystate = States.Options; } // jump to WrongOption3 not the Options state;
x++;
return x;
}
void WrongOption3() { text.text = "You Lost"; }
void CoretOption() { text.text = "You Win!"; }
}
=================================================
In Wrongoption() function, if i press R to return, it directly jump to the WrongOption3 state even it is my first time to choose the wrong option. It didn’t do any counting.
I’m using Unity 5.4.1f1, and Visual Studio Community 2015 V14.0.25123.
I really appreciate if any one can help me to solve this problem. Thank you.