- Home /
Instantiate Particles, WaitForSeconds and continue
Hi,
I have been working with trying to achieve the following:
1) Player Steps onto a trigger
2) Player Presses a key while on trigger to Instantiate particles
3) Particles instantiate
4) Start to WaitForSeconds(3.5f)
5) End of 3.5 seconds->Fade Particles out by code (ParticleAnimator)
6) WaitForSeconds(3.5f)
7) End of 3.5 seconds->Reinstantiate Particles (may be unecessary)
Steps 4 - 7 should continue doing so until the Player moves i.e. (Mathf.Abs(PlayerMoveScript.MoveForward) > 0)) even if Player remains on trigger.
I can start by showing the code for when the player presses a key to instantiate particles (assuming everything is declared):
OnTriggerStay(Collider other) { void InstantiateParticles() { if (Input.GetKeyDown("space") && PlayerMoveScript.MoveForward == 0) { if (RedParticles != null && theRedParticles == null) { Debug.Log("Preparing to instantiate"); theRedParticles = (GameObject) Instantiate(RedParticles, transform.position, Quaternion.identity); newRedParticles = (GameObject) RedParticles;
StartCoroutine(FadeParticles());
}
}
}
} private IEnumerator FadeParticles() { yield return StartCoroutine(Wait())
if (theRedParticles != null)
{
holder = new Color[5];
holder = theRedParticles.GetComponent<ParticleAnimator>( ).colorAnimation;
for (int x=0; x<5; x++)
holder[x].a -= 0.02f;
theRedParticles.GetComponent<ParticleAnimator>( ).colorAnimation = holder;
yield return StartCoroutine(Wait());
Debug.Log("Done Waiting Again..");
// try to use another prefab with the same qualities.
theRedParticles = newRedParticles;
}
yield return 0;
}
private IEnumerator Wait() { yield return new WaitForSeconds(3.5f); }
Revised code Result: The code does not wait for 3.5 seconds, for some reason.
Answer by Daniel 6 · Jul 10, 2010 at 10:12 PM
Just quickly looking at your code I noticed one issue. You have a static IEnumerator method and that doesn't work. Methods with yield commands can not be static.
I appreciate your help, but it does not appear to be waiting for 3.5 seconds (even if I do a * Time.deltaTime)
I forgot yeild return StartCoroutine(FadeParticles()). And the particles happen to not fade as I intended. Any suggetions for that? Or would I need to make another post?
Your answer
Follow this Question
Related Questions
WaitForSeconds does not work 0 Answers
yield return waitforseconds not waiting? 3 Answers
Respawn Time 1 Answer
WaitForSeconds in virtual void. having trouble understanding IEnumerator 1 Answer