Active and deactive Gameobjects.

When i click on a button i want to active and deactive gameobjects continously like a blink effect. This is the script i am using

public GameObject cube;
	
	public bool isBlink;
	
	void Start () 
	{
		cube = GameObject.Find("Cube");
	}
	
	void Update () 
	{
		if(isBlink)
		{
			StartCoroutine("BlinkGameObject");
		}
	}
	
	void OnGUI()
	{
		if(GUI.Button(new Rect(100, 100, 100, 50), "Blink"))
		{
			isBlink = true;	
		}
	}
	
	IEnumerator BlinkGameObject()
	{
		yield return new WaitForSeconds(2f);
		
		cube.SetActive(!cube.activeSelf);
}
}

It is blinking but very fast. I want to slow it down like a normal blink effect.

Your BlinkGameObject() co-routine is being continuously called since isBlink always remains true after the first button press.I guess the code below will do for a blink effect :

    bool isBlink;
    float blinkTimer;
    void Start () 
    {
       isBlink = false;
       blinkTimer = 2.0f;
    }
 
    void Update () 
    {
       if(isBlink)
       {
         	StartCoroutine("BlinkGameObject");
			isBlink = false;
       }
    }
 
    void OnGUI()
    {
       if(GUI.Button(new Rect(100, 100, 100, 50), "Blink"))
       {
         isBlink = true;  
       }
    }
 
    IEnumerator BlinkGameObject()
    {
       	yield return new WaitForSeconds(blinkTimer);
 
       	renderer.enabled = !renderer.isVisible;
		StartCoroutine("BlinkGameObject");
	}

Adjust blink timer according to your needs.

in your BlinkGameObject() method I think by looking at it you need to add another line or two:

IEnumerator BlinkGameObject()
{
    isBlink = false;
    yield return new WaitForSeconds(2);
    cube.SetActive (!cube.activeSelf);

    // isBlink = true;
}

I’ve commented out the last bit as I’m not sure if you need it. Whats happening is the update function is called many many times per second (hundred I believe) and when you click your gui button isBlink is true, whcih means update is constantly calling BlinkGameObject() over and over.

Hope this helps!

Oh dear - so many answers to this question - all of them a bit wrong :S

So:

  • There is no need at all to start the coroutine from Update, you should start it from the GUI. Calling from Update just means you keep creating new copies and adds unnecessary spaghetti.

  • You should probably blink the renderer not the whole game object

      public GameObject cube;
    
     public bool isBlink;
    
     void Start () 
     {
        cube = GameObject.Find("Cube");
     }
    
    
     void OnGUI()
     {
        if(GUI.Button(new Rect(100, 100, 100, 50), "Blink"))
        {
          StartCoroutine(BlinkGameObject());
        }
     }
    
     IEnumerator BlinkGameObject()
     {
        if(isBlink) yield break;
        var cubeRenderer = cube.GetComponentInChildren<Renderer>();
        isBlink = true;
        while(isBlink)
        {
             yield return new WaitForSeconds(2f);
             cubeRenderer.enabled = !cubeRenderer.enabled;
        }
    
     }
    
    }
    

I am used to JS so this might be wrong but why do you have an “f” in WaitForSeconds?
any way:
Try doing this instead

 IEnumerator BlinkGameObject()
    {
       IsBlink = false;
       yield return new WaitForSeconds(50);
 
       cube.SetActive(!cube.activeSelf);
       IsBlink = true;
    }

the problem is that you are calling “BlinkGameObject” every frame. The wait only happens the first time you load it. This way it will be loaded, turn “IsBlink” off, so it doesn’t load that again, and then do it’s stuff and set “IsBlink” to true again… Try it… It’s not tested but shoud work…

Good luck :wink: