- Home /
Getting Buttons to change stage c# Add Listner
So I am trying to get a basic structure for a text based game but at the moment it is set up for windows, so I have the code below
void cell () {
text.text = "You are in a prison cell, and you want to escape. There are " +
"some dirty sheets on the bed, a mirror on the wall, and the door " +
"is locked from the outside.\n\n" +
"Press S to view Sheets, M to view Mirror and L to view Lock" ;
if (Input.GetKeyDown(KeyCode.S)) {myState = States.sheets_0;}
else if (Input.GetKeyDown(KeyCode.M)) {myState = States.mirror;}
else if (Input.GetKeyDown(KeyCode.L)) {myState = States.lock_0;}
}
So if I want to make this work for Android I created 3 UI Buttons A, B and C and I made the buttons public doing the below in the same script
public Button a;
public Button b;
public Button c;
So then if I want to change the (Input.GetKeyDown(KeyCode.S)) {myState = States.sheets_0;} to change the state with a button press I tried doing a.onClick.AddListener(() => { myState = YOUR_STATE_HERE; });
however it does not seem to work, any help and suggestions on how I can make my button A B and C input change my state to work on android ?
Thank you
Answer by Deathdefy · May 05, 2017 at 04:59 PM
You can add click events through the inspector or through C#. Here is the C# solution
public void Start()
{
Button a;
Button b;
Button c;
a.onClick.AddListener(() => ChangeState(States.sheets_0));
b.onClick.AddListener(() => ChangeState(States.mirror));
c.onClick.AddListener(() => ChangeState(States.lock_0));
}
public void ChangeState(States state)
{
myState = state;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612121049im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I make my player jump on click on the button? 3 Answers
Ball moving much faster forward when there is horizontal input 0 Answers
can i "click" a button by pressing a key or gamepad button? 1 Answer
why cant I get android accelerometer working in two planes? 3 Answers
Android game crashes on startup. " Unable to find libc" 1 Answer