- Home /
Blink objects at a constant rate
Hi, I'm new to Unity and I'm trying to have an object blink rapidly at a constant rate, say 15 Hz, that I can set in the editor. So far, I've tried having the object activate and deactivate:
public GameObject Block;
public float frequency;
void FixedUpdate ()
{
InvokeRepeating ("FlickerBlock", 0, frequency);
}
void FlickerBlock ()
{
if (Block.activeSelf)
Block.SetActive (false);
else
Block.SetActive (true);
}
However, when I play this, the object doesn't seem to blink at a constant rate. It blinks faster at some points and seems to skip at other points. More concerning is that after maybe a minute or two, the FPS drops drastically from ~60-70 to ~20-30. I'm not sure what is causing this as I only have two stationary cubes doing this in the scene so far. Is there a way to make Unity run this more efficiently? Would it be better to have the objects blink through some other way than activating/deactivating it?
Answer by abilius_xxi · May 04, 2015 at 09:34 AM
You are generaring a new flow/sequence of blink calls with each new physics cycle because it's called inside fixedupdate. Hence your yields drops dramatically after a minute.
Try calling repeatinginvoke from outside fixedupdate.
Answer by Calum1015 · May 02, 2015 at 06:29 AM
Use InvokeRepeating to call a function repeatedly. It might be a little inefficient but then in the function you just called, Use Invoke to call another function once this time to disable the object(s). Is this what you were looking for?
Your answer
Follow this Question
Related Questions
Are scripts on a disabled game object disabled aswell? 1 Answer
Activate Game object when in Camera view and deactivate when out of camera view. 2 Answers
Active and deactive Gameobjects. 4 Answers
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
ReActivate object with null movement ?? 2 Answers