GameOver scene loads whenever I touch anywhere on the screen

My code:

var incorrect: AudioClip;

var correct: AudioClip;

var anything: GameObject;

var diamond: Sprite;

var circle: Sprite;

var triangle: Sprite;

var square: Sprite;

var number: int;

var AcceptInput: boolean = true;

static var score: int = 1;

var guiScore: GUIText;


function Start() {

  number = Random.Range(1, 4);

  if (number == 1) {

    anything.GetComponent(SpriteRenderer).sprite = diamond;

  } else if (number == 2) {

    anything.GetComponent(SpriteRenderer).sprite = circle;

  } else if (number == 3) {

    anything.GetComponent(SpriteRenderer).sprite = triangle;

  } else {

    anything.GetComponent(SpriteRenderer).sprite = square;

  }
}


function Update() {

  if (Input.GetMouseButtonDown(0)) {

    if (AcceptInput) {

      AcceptInput = false;

      Debug.Log("Clicked");

      if (anything.GetComponent(SpriteRenderer).sprite == diamond) {

        audio.PlayOneShot(correct);

        guiScore.text = "Score: " + score;

        StartCoroutine("YieldTestEnumerator");

        AcceptInput = true;

        number = Random.Range(1, 4);

        if (number == 1) {

          anything.GetComponent(SpriteRenderer).sprite = diamond;

        } else if (number == 2) {

          anything.GetComponent(SpriteRenderer).sprite = circle;

        } else if (number == 3) {

          anything.GetComponent(SpriteRenderer).sprite = triangle;

        } else {

          anything.GetComponent(SpriteRenderer).sprite = square;

        }

      } else if (anything.GetComponent(SpriteRenderer).sprite == circle) {

        Debug.Log("Wrong Answer!");

        Application.LoadLevel("GameOver");

        audio.PlayOneShot(incorrect);

      } else if (anything.GetComponent(SpriteRenderer).sprite == triangle) {

        Debug.Log("Wrong Answer!");

        Application.LoadLevel("GameOver");

        audio.PlayOneShot(incorrect);

      } else if (anything.GetComponent(SpriteRenderer).sprite == square) {

        Debug.Log("Wrong Answer!");

        Application.LoadLevel("GameOver");

        audio.PlayOneShot(incorrect);

      }
    }
  }
}

function YieldTestEnumerator() {
  yield WaitForSeconds(0.5);
}

So as you can see its a multiple choice question game. The script is attached to a button. So when you press the button, it would check if the sprite of another object( the variable “anything”) is diamond. If it is then add score if not load gameover scene. But right now, if I press anywhere in the scene it would just pop up the game over scene. I tried to add colliders to it but nothing happened. Though, if I press the button and the sprite is correct sometimes it would load GameOver scene but sometime it is normal. Why would it detect a touch from anywhere on the screen? Does anyone have any ideas? Thanks in advance

Input.GetMouseButtonDown(0) takes a mouse button press anywhere on the screen that’s why the code inside it is working on click anywhere on the screen.

Solution:

  • If you are using a GUI.Button then you should put your code inside the Input.GetMouseButtonDown(0) to be inside the

    if(GUI.Button(…)) { }

  • If you are using a GUITexture as a button then you can use GUIElement.HitTest to check if the click is inside the area of the button and then execute the code.

  • If you are using a 3D object as a button then you can use Raycast to check if the collider of that button is hit and perform the related action.