- Home /
Particle Emitter Layer Issue
I have had this minor issue for sometime, but now that I am about to release my game I would like to clean it up. I have a lot of particle emitters and a few ellipsoid emitters. The particle emitters work no problem. Although, when using the ellipsoid particle emitter, the first time the code is called, to instantiate the emitter, it does not show on screen. After the first one is finished, the remaining ellipsoid particle emitters work with no issue. At first I had the issue where the particles would spawn under the background, but in a recent update I made sure all of the layers were set to "foreground" (which is in front of everything) and wrote some code to set the particle's layer to foreground whenever it was active in the scene.
Here's my code:
using UnityEngine;
using System.Collections;
public class TimeChangingPowerUp : MonoBehaviour {
public GameObject particle;
public AudioClip powerUpSound;
public static float time=20f;
public static float speed=0.5f;
public static float duration = time * speed;
public static bool Active=false;
void Start(){
Time.timeScale = 1f;
}
void Update(){
if (Active==false) {
StartCoroutine (DestroyGameObject());
}
}
IEnumerator SlowMoRoutine(float time, float speed)
{
Active = true;
gameObject.GetComponent <SpriteRenderer > ().enabled = false;
gameObject.GetComponent <CircleCollider2D > ().enabled = false;
GameObject clonedParticle;
clonedParticle =Instantiate (particle, transform.position, transform.rotation) as GameObject ;
GameObject sound = new GameObject();
sound.transform.position = transform.position;
AudioSource audioSource = sound.AddComponent<AudioSource>();
audioSource.clip = powerUpSound;
audioSource.Play();
Destroy (clonedParticle,2f);
Destroy (sound,powerUpSound .length);
Time.timeScale = speed;
yield return new WaitForSeconds (duration);
Time.timeScale = 1f;
Destroy (gameObject);
}
IEnumerator DestroyGameObject(){
yield return new WaitForSeconds (3);
gameObject.GetComponent <SpriteRenderer > ().enabled = false;
gameObject.GetComponent <CircleCollider2D > ().enabled = false;
yield return new WaitForSeconds (17);
Destroy (gameObject);
}
void OnTriggerEnter2D(Collider2D other){
if (other.tag == "Player") {
Active = true;
StartCoroutine (SlowMoRoutine (time, speed));
}
}
}
Your answer