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