- Home /
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.
Answer by whydoidoit · May 08, 2013 at 07:59 AM
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; } } }
Answer by VivekD · May 08, 2013 at 07:12 AM
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.
Answer by SubatomicHero · May 08, 2013 at 07:16 AM
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!
Answer by CDK · May 08, 2013 at 07:55 AM
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 ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220613101820im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
indestructible gameobject 0 Answers
Why are my 2D sprite GameObjects blinking and how do I fix this problem? 0 Answers
Blink objects at a constant rate 2 Answers
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Use an objects (from array) position to focus a camera on 3 Answers