- Home /
Trying to make a game that tests whether the player has pressed on the left or on the right side of the screen but it doesn't work
The game is supposed to be a very unoriginal coordination game, where there's an arrow and a text displayed on the screen (both of them either display left or right randomly) and you have to click on the side that the text is showing.
IEnumerator Game() {
timeLeft = 15f;
int randomNumberArrow = Random.Range (-1, 2); // Determines whether the ARROW shows left or right
int randomNumberText = Random.Range (-1, 2); // Determines whether the TEXT shows left or right
if (randomNumberArrow == 0) { // ARROW shows LEFT
arrow.transform.rotation = Quaternion.Euler(0f, 0f, 180f);
} else { // ARROW shows RIGHT
arrow.transform.rotation = Quaternion.Euler(0f, 0f, 0f);
}
if (randomNumberText == 0) { // TEXT shows LEFT
leftOrRight.text = "LEFT";
if (Input.GetMouseButtonDown(0)) { // Tests if the player taps the correct(left) side
if (Input.mousePosition.x < 240) {
pressedCorrectly = true;
} else {
pressedCorrectly = false;
}
}
} else { // TEXT shows RIGHT
leftOrRight.text = "RIGHT";
if (Input.GetMouseButtonDown(0)) { // Tests if the player taps the correct(right) side
if (Input.mousePosition.x > 240) {
pressedCorrectly = true;
} else {
pressedCorrectly = false;
}
}
}
if (pressedCorrectly) { // Player Wins
StartCoroutine ("Game", 0.1f);
score++;
Debug.Log (score);
} else { // Player Fails by clicking incorrectly
GameOver ();
StopCoroutine ("Game");
}
yield return new WaitForSeconds (timeLeft);
if ( timeLeft <= 0 ) // Player Fails by not clicking in time
{
GameOver();
StopCoroutine ("Game");
}
}
When I hit play I instantly get the Game Over screen. I believe this has something to do with the if statements (where the "TEXT shows RIGHT" and "TEXT shows LEFT" comments are). Note that 240 is the x position that's in the center of the screen.
Answer by Taylor-Libonati · Dec 27, 2017 at 09:12 PM
Is this a school project? It looks like you are trying to create your own game loop but that's not really how Unity works. You should be creating a monobehaviour script that attaches to a game object and then just handle your game logic in the Update loop.
But that aside, here is your problem. You are checking your random numbers and checking the input then at line 31 you check if they pressed the right button but its now only been 1 frame in the game. So of course no one can hit the button in 1 frame so it goes to a game over. If they did somehow have the mouse down when you hit play it would start another game loop on top of the one you are in, which is also not what you want.
I would suggest doing a couple unity scripting tutorials to figure out how to properly use the Update loop. But to make what you have work you would have to not do anything (keep looping) until a button is pressed. So like have a bool that is buttonPressed and it is only true if anything has been pressed. If it is true then you check pressedCorrectly. If it is false you just continue the loop.
Something like this:
while(!buttonPressed){
//All your input stuff
}
if(pressedCorrectly){
//The rest of the stuff
Not a school project, more like a challenge for myself. Thanks! I'll check out the scripting tutorials right now.
Your answer
![](https://koobas.hobune.stream/wayback/20220612145329im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
If Loop For Key Press works but not for Mouse 0 Answers
Why simple letter evaluataion returns true no matter witch letter 1 Answer
How to resize a player every couple of seconds? 2 Answers
Problem when checking a Quaternion Eulerangle axis 1 Answer
[SOLVED]Different levels of damage per particle collision 1 Answer