• 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 Dreeka · Mar 02, 2011 at 07:22 PM · javascriptblinking

My blinking script is not working

Hello!

I would like an object to blink for 3 seconds. I have wrote the following code, but Unity is not entering to function blinking(). What do I do wrong?

var blinkTime : int = 3; //How long the sphere will blink

function Start() { InvokeRepeating("blinking", 1, 1); }

function blinking() { print(blinkTime); blinkTime--; print(blinkTime); if (blinkTime == 0) { Object.Destroy(Object.name); } else { yield WaitForSeconds(0.5); renderer.enabled = false; yield WaitForSeconds(0.5); renderer.enabled = true; } }

Thanks!

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 Eric5h5 · Mar 02, 2011 at 07:38 PM 2
Share

Format your code, and also, you should pick either UnityAnswers or the forums; there is no need for cross-posting.

avatar image Bunny83 · Mar 02, 2011 at 11:00 PM 0
Share

Didn't you get compiler errors?

avatar image Joshua · Mar 02, 2011 at 11:01 PM 0
Share

You'r blinking function is not working because there's no capital first letter! =p

avatar image Bunny83 · Mar 02, 2011 at 11:13 PM 0
Share

No the capital letter is just common program$$anonymous$$g style. That's not the problem. But i've just tried it and i think i can remember that you can't use coroutines with Invoke or InvokeRepeating... You should just use a coroutine. I will edit my answer

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Mar 02, 2011 at 10:59 PM

The only thing i can see is this line:

Object.Destroy(Object.name);

It's almost completely wrong ;)

  1. There is not version of Destroy that takes a string as parameter. You have to pass an object here.
  2. Object is a class, not a reference to a particular object so you can't use "name" of the class.
  3. Just a side note: You don't have to write Object before Destroy. Only in classes that do not inherit from UnityEngine.Object. Anyways, it's not wrong so you can do this if it helps your understanding of your code.

If you want to remove this script from the gameobject use:

Destroy(this);

If you want to destroy the whole GameObject use:

Destroy(gameObject);


edit
The reason why this doesn't work is that you can't use coroutines with Invoke or InvokeRepeating. You can use a single coroutine to achieve your desired behaviour.

var blinkTime : int = 3; //How long the sphere will blink

function Start() { yield WaitForSeconds(1); // The initial delay that you passed to InvokeRepeating while (blinkTime > 0) { blinkTime--; yield WaitForSeconds(0.5); renderer.enabled = false; yield WaitForSeconds(0.5); renderer.enabled = true; } yield WaitForSeconds(1); // wait another sec. Destroy(gameObject); }

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

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

Script not working (Java script) 2 Answers

How to Select between two prefabs randomly ? 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