- Home /
Weird thing with my script :/
heres my script :
var anything : GameObject;
var diamond : Sprite;
var circle : Sprite;
var triangle : Sprite;
var square : Sprite;
var number : int;
var target : Timer;
var AcceptInput : boolean = true;
static var score : int = 0;
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;
target.enabled = true;
Debug.Log("Clicked");
if(anything.GetComponent(SpriteRenderer).sprite == diamond) {
score += 1;
guiScore.text = "Score: " + score;
StartCoroutine("YieldTestEnumerator");
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;
}
}
if(anything.GetComponent(SpriteRenderer).sprite == circle) {
Debug.Log("Wrong Answer!");
Application.LoadLevel("GameOver");
}
if(anything.GetComponent(SpriteRenderer).sprite == triangle) {
Debug.Log("Wrong Answer!");
Application.LoadLevel("GameOver");
}
if(anything.GetComponent(SpriteRenderer).sprite == square) {
Debug.Log("Wrong Answer!");
Application.LoadLevel("GameOver");
}
if(Input.GetMouseButtonUp(0)) {
AcceptInput = true;
}
}
}
}
function YieldTestEnumerator () {
yield WaitForSeconds (0.5);
}
So basically this is a multiple choice game. Right now, if I press the button, although the sprite is diamond, it would add a point then it would load level GameOver. I don't get this. Does anyone have any ideas? Thank you very much
(reformat your code because it's impossible to read for us in this form)
How do I reformat it and what do I reformat it into? @andrew-lukasik
I mean it would be really more convenient to read your code when it would more like this:
if (anything.GetComponent(SpriteRenderer).sprite == circle) {
Debug.Log("Wrong Answer!");
Application.LoadLevel("GameOver");
}
if (anything.GetComponent(SpriteRenderer).sprite == triangle) {
Debug.Log("Wrong Answer!");
Application.LoadLevel("GameOver");
}
$$anonymous$$uch shorter and clearer. I looks reaaaly long and complicated right now where it's not that much
Answer by $$anonymous$$ · Aug 21, 2014 at 11:59 AM
I have a fair few things to point out here.
Firstly, it would be much easier to help you if you provide a little bit more information, and correctly formatted your code.
Secondly, you should look up arrays at some stage.
Finally, to answer your question, you are confused as to why it still says game over when you pick the diamond?
This is because when you get the correct answer, it is then choosing a new sprite. You are then checking if it is an incorrect sprite.
Essentially, it is saying you got it correct, then changing it to something incorrect, then saying you got it incorrect.
Try changing your:
if (sprite is diamond)
blah
if (sprite is square)
blah
etc.
to
if (sprite is diamond)
blah
else if (sprite is square)
blah
etc.
thanks now its fixed but theres another weird thing after I changed it to else if, if it is the right answer, it would add 2 score and then I can't press the button. This is really weird. Do you have any ideas?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
BCE0049 error with network script 0 Answers
Need help with WeaponSwitch Script 0 Answers