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
-1
Question by markzareal · Aug 07, 2014 at 10:31 AM · javascript

How to restrict a button for a second

I know I kind of asked this question one time, but no-one answered me correctly. Firstly Heres my script

 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)
 {
 return;
 }
 AcceptInput = false;
 
 
 targetScript = GameObject.Find("Shape").GetComponent(ChangeSprite);
 
 
 GameObject.Find("Shape").GetComponent(ChangeSprite).enabled = true;
 Debug.Log("Clicked");
 
 if(targetScript.spriteRenderer.sprite == targetScript.diamond) {
 AcceptInput = true;
 score += 1;
 guiScore.text = "Score: " + score;
 }
 
 
 else{
 Debug.Log("Wrong Answer!");
 }
 
 }
 
 function OnMouseUp () {
 AcceptInput = true;
 GameObject.Find("Shape").GetComponent(ChangeSprite).enabled = false;
 }

and the script that is being enabled :

 var diamond : Sprite;
 var circle : Sprite;
 var triangle : Sprite;
 var square : Sprite;
 var number : int;
 var spriteRenderer : SpriteRenderer;
 
 function OnEnable() {
 
 spriteRenderer = gameObject.GetComponent(SpriteRenderer);
 
 number = Random.Range(1,5);
 
 if(number == 1) {
 spriteRenderer.sprite = diamond;
 }
 
 else if(number == 2) {
 spriteRenderer.sprite = circle;
 }
 
 else if (number == 3) {
 spriteRenderer.sprite = triangle;
 }
 
 else {
 spriteRenderer.sprite = square;
 }
 
 
 }
 
 function OnEnable() {
 
 spriteRenderer = gameObject.GetComponent(SpriteRenderer);
 
 number = Random.Range(1,5);
 
 if(number == 1) {
 spriteRenderer.sprite = diamond;
 }
 
 else if(number == 2) {
 spriteRenderer.sprite = circle;
 }
 
 else if (number == 3) {
 spriteRenderer.sprite = triangle;
 }
 
 else {
 spriteRenderer.sprite = square;
 }
 
 
 }

What the script does is OnMouseDown, it enables a disabled script which changes an object's sprite. And if the sprite is the correct answer, it would add a score. If not, Debug.Log("wrong Answer!") I used the accept input thing because if I don't it would play through the sprites non stop because the script wouldn't be disabled. But the thing is, if I press the button, it would add the score before the sprite would even be displayed on the screen, so I want to make a restriction so that you can only press the button and the button can only work when the actual sprite is displayed. Does anyone have any Real ideas? Thank You

Comment
Add comment · Show 4
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 08, 2014 at 06:32 AM 0
Share

Anyone have any ideas?!!?

avatar image Kiwasi · Aug 08, 2014 at 06:36 AM 0
Share

You're question is difficult to understand.

But if I'm getting you right you just want delayed functionality. One way to do this is to use a coroutine and wait. Another is to use Invoke

avatar image markzareal · Aug 08, 2014 at 08:27 AM 0
Share

I tried WaitForSeconds but didn't really work. I want it to be the button only works when the sprite is displayed. Currently, the score is adding before the sprite is displayed. @Bored$$anonymous$$ormon

avatar image markzareal · Aug 08, 2014 at 11:35 AM 0
Share

Guys do you have any ideas?!?!?!?!?!?!?!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kannan21 · Aug 08, 2014 at 06:36 AM

Change OnEnable function to Start.

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 08, 2014 at 08:28 AM 0
Share

Isn't it the same? since the script is going to be disabled after the On$$anonymous$$ouseUp

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Multiple choice game not working!?! 1 Answer

Script behaving too early 1 Answer

Nested Functions 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 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