How to combine a if statement with whether or not a certain button was clicked?
Hello there, we have a project for university and i am totally new to unity. We have to create a street map with buttons on it. In the quiz mode, a question is shown in the dialogue panel. The player then has to select the right button that answers the question right. e.g. "It is raining and you don't want to walk to the city. Where do you go?" The right answer would then be to click on the bus button. After three trials, the wright button would start flashing.
I posted a picture for better understanding. It is in German - sorry for that!
My question now is: How do i create a script, that
1. prints "Great" in the dialogue Panel if the right button is clicked (here it would be the bus button)
2. Prints "Try again" if a wrong button is clicked
3. know when to flash the right button (after three misses). Thanks so much! I tried so many ways but it just won't work.
Answer by Matt1000 · Mar 16, 2017 at 09:09 PM
Buttons are themselves if statements, you can actually do this:
public void OnGUI () {
if (GUI.Button (new Rect (position, size), texture)) {
//Code
}
}
If you need to check if the answer is right i would recommend using an enum. You should create an enum with all your possible buttons/answers and then have a variable which iis the right one:
public enum Places {
BusStation,
Hospital,
Hotel,
//Etc.
}
Places rightAnswer = Places.BusStation;
Places answer;
public void OnGUI () {
//Bus Station Button
if (GUI.Button (new Rect (position, size), busStationTexture)) {
answer = Places.BusStation;
if (answer == rightAnswer) {
//Great!
} else {
//Try Again
}
}
//Bus Hospital
if (GUI.Button (new Rect (position1, size), hospitalTexture)) {
answer = Places.Hospital;
if (answer == rightAnswer) {
//Great!
} else {
//Try Again
}
}
//Bus Hotel
if (GUI.Button (new Rect (position2, size), hotelTexture)) {
answer = Places.Hotel;
if (answer == rightAnswer) {
//Great!
} else {
//Try Again
}
}
}
Surely not the nicest way to do it but that's the idea :)