Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by markzareal · Aug 21, 2014 at 10:01 AM · javascript

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrew-lukasik · Aug 21, 2014 at 10:16 AM 0
Share

(reformat your code because it's impossible to read for us in this form)

avatar image markzareal · Aug 21, 2014 at 11:42 AM 0
Share

How do I reformat it and what do I reformat it into? @andrew-lukasik

avatar image andrew-lukasik · Aug 21, 2014 at 11:50 AM 1
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.





Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image markzareal · Aug 22, 2014 at 06:04 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges