[Solved] Adding button input instead of keyboard inputs

I have finished S04, and now I know how to add UI buttons. So now instead of giving a keyboard input to the text adventure game, I want to add a UI button input.

My script looks like this:

public void state1 () {
		Question.text = ("Q1");
		Choice1.text = ("text1") ;
		Choice2.text = ("text2");
		Choice3.text = ("text3");
    }

I have 4 UI texts which changes depending on the state.
3 of the UI text have been made to a button the “Choice#” texts.

Problem
I want to add a UI button which will change state depending on what state it currently is in.
I could add the following code directly under the state and it will work for keyboard input. So if I put the following code, pressing R would take me to state2 only if it is in state1.

if (Input.GetKeyDown(KeyCode.R)) {myState = States.state2;}

Adding a UI button, I am forced to make a new method. So how can I make the UI button change the state depending on what state it is in currently.

Solution

So what I did was,

public void Button1 () {
		if 		(MyState == States.state1) {MyState = States.state2;}
		else if (MyState == States.state3) {MyState = States.state4;}
	}

So now if in state1 button takes me to state2, if in state3 it takes me to state4.
It is a bit more complicated than adding a keyboard input. I was having trouble making the button do a different thing each time.

If there is an easier way please tell me, thank you. :slight_smile:

Sorry for not making myself clearer the first time. newbie here. I hope I have made myself a little bit clearer now.

As you would know now, you can wire the button’s OnClick() method to another method. I’m improvising here, but you’d roughly want something like:

  • Add a new Button for your R keypress

  • Refactor the KeyCode.R code into a new common method for R presses, e.g.:

if (Input.GetKeyDown(KeyCode.R)) { OnPressR(); }

// ...

public void OnPressR() {
  myState = States.state2;
}
  • Wire the OnPressR() method to the OnClick action on your new button.

Now either the button click or a keypress will fire the same code in OnPressR().

1 Like

Just wanted to correct this too, FYI. The term is method, not void. When you declare a method such as:

public void DoSomething() {

you’re saying “OK I’m declaring a method called DoSomething, and it will be publicly accessible (public), and it takes no parameters (the empty parentheses () ), and it returns nothing to the method that called it (void).”

If you replaced void with an object type such as string, you would change the last phrase of the declaration to “… and it returns a string to the method that called it”.

2 Likes

Thank you for the help.

But I think I did not make my problem clear.

What I was trying to do was, make a button which will do different thing depending on the state. And I have finally managed to this by

public void Button1 () {

	if 		(MyState == States.states1) {MyState = States.states3;}
	else if      (MyState == States.state2) {MyState = States.states4;}
}

Privacy & Terms