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 03, 2014 at 03:59 AM · javascript

Script behaving too early

My script is behaving too early when clicked. Here's my code:

  var targetScript : ChangeSprite;
  var AcceptInput : boolean = true;
  private static var score : int = 0;
  var guiScore : GUIText;
     
 function Start ()
 {
 guiScore.text = "Score: 0";
 }
 
 
 function OnMouseDown () {
 if(AcceptInput)
 {
 AcceptInput = false;
 }
 
 targetScript = GameObject.Find("Shape").GetComponent(ChangeSprite);
 
 
 GameObject.Find("Shape").GetComponent(ChangeSprite).enabled = true;
 Debug.Log("Clicked");
 
 if(targetScript.spriteRenderer.sprite == targetScript.diamond) {
 score += 1;
 guiScore.text = "Score: " + score;
 }
 else{
 Debug.Log("Wrong Answer!");
 }
 
 }
 
 function OnMouseUp () {
 AcceptInput = true;
 GameObject.Find("Shape").GetComponent(ChangeSprite).enabled = false;
 }

It changes the sprite too early. When I press the button it would add the score before it even displayed the sprite. Any ideas? Any help would be much appreciated. Thanks

Comment
Add comment · Show 6
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 NoseKills · Aug 03, 2014 at 07:34 AM 0
Share

What is the behavior you want to have?

Right now you are adding the score and displaying a sprite in the same method call so the next time the screen is rendered after $$anonymous$$ouse Down happens, the increased score will be shown with the sprite.

If you want to have e.g. a one second delay you have to specifically program it because the screen doesn't get redrawn in between the lines of code in the On$$anonymous$$ouseDown function.

avatar image gjf · Aug 03, 2014 at 09:14 AM 0
Share

why have you used the AcceptInput bool?

you set it to true in On$$anonymous$$ouseUp() but On$$anonymous$$ouseDown() just does this

 if(AcceptInput)
 {
     AcceptInput = false;
 }

which is the same as

 AcceptInput = false;

AND you don't do anything depending on it's value.

did you intend for the other 'logic' to run only if it's true?

also, perfor$$anonymous$$g GetComponent() is a relatively slow operation to perform many times. consider moving this line to Awake()/Start()

 targetScript = GameObject.Find("Shape").GetComponent(ChangeSprite);

and change other uses of

 GameObject.Find("Shape").GetComponent(ChangeSprite)

to

 targetScript






avatar image markzareal · Aug 03, 2014 at 09:20 AM 0
Share

I used the accept input boolean so it doesn't flash through sprites if i hold it. @gjf

avatar image markzareal · Aug 03, 2014 at 09:22 AM 0
Share

how can you add a delay to a behavier? @Nose$$anonymous$$ills

avatar image gjf · Aug 03, 2014 at 09:30 AM 0
Share

the code you have is the same as setting it to false every time. are you using it somewhere else?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KellyThomas · Aug 03, 2014 at 09:43 AM

If you are using AcceptInput to ensure that the code in OnMouseDown() is not called repeatedly I suggest using a lock like this:

 function OnMouseDown () {
     if(!AcceptInput){
         return;
     }
     AcceptInput = false;
     // ...
 }

 function OnMouseUp () {
     AcceptInput = true;
     // ...
 }
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 03, 2014 at 10:03 AM 0
Share

Does that make the score behave later?

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

24 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 avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

How to restrict a button for a second 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to save the best score 1 Answer

Multiple choice game not working!?! 1 Answer


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