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!

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);
}