Particles vanishing after setting parent to null. Any Fix?
Hey there,
First time ever logging into the website, I hope this is the right place to put this question. For background, I'm an intermediate Unity user, I've been using it for about two years, and have been programming for about 3. Anyway, here's the problem:
I actually used the 2D engine for the first time yesterday, and I was messing around on it today. The problem occurs when I have a projectile (it's a net), which gets instantiated by the player script attatched to the player gameobject. Upon either colliding with a non-player object OR reaching the 3 second timer, the net projectile will self-destruct.
In order to spruce it up, I have a particle system attached to the net, with the intention of detaching it (clearing the parent) in the net's self-destructing function (named HitDestroy). About at the same time, I am calling ParticleSystem.Play() on the emitter, so that it will start playing at that point, since PlayOnAwake is false and looping is false and it hasn't otherwise been playing. The code looks like this for the Net class:
using UnityEngine; using System.Collections;
public class Net : MonoBehaviour {
private Transform tf;
public ParticleSystem part;
[HideInInspector]
public int direction = 0;
private float speed = 0.1f;
void Start () {
tf = transform;
StartCoroutine ("deathTimer");
if (direction == 0) {
tf.GetComponent<SpriteRenderer> ().flipX = true;
}
}
void Update () {
tf.Translate(new Vector3(((direction - 0.5f) * 2f) * speed,0f,0f));
}
private IEnumerator deathTimer(){
yield return new WaitForSeconds (3);
HitDestroy ();
}
void OnTriggerEnter2D(Collider2D other){
if (other.gameObject.tag != "Player") {
HitDestroy ();
}
}
void HitDestroy(){
part.Play ();
part.transform.SetParent (null);
Destroy (part.gameObject,2.1f);
Destroy (gameObject);
}
}
The problem seems to be occurring in HitDestroy, since all of the other code works just fine. When the particle system's transform has its parent set to null (either through SetParent or tf.parent = null), the particles vanish. According to the triangle count and GetParticles calls on the emitter, the particles do exist, they are being spawned. Strangely, though, they do not appear. I've checked everything i could think of, and I've been at it for a good 3 hours. Scoured official and unofficial forums extensively, still can find nothing. Since it's 2D, i've tried adjusting sorting layer, order in sorting layer as well. I've ensured that the scale is not fudging up the system (its a pretty basic particle system).
I've attempted rearranging the lines in HitDestroy, figuring it might not be possible (for some reason) to call Play after detaching the GameObject that the particle system component is on.
I recognize that there are multiple ways to have a particle system spawn when an object needs to be destroyed/disabled. In all of the projects I play around with, I've done that using at least 3 different methods (like actual instantiation). The point of this is that I specifically want part.Play (); part.transform.SetParent (null); Destroy (part.gameObject,2.1f); to be called concisely and within a single function/method of the Net class. The reason I'm being relatively stubborn with this process is that, being a computer science major (and a crummy one at that) I want to learn exactly why this isn't working, and I'm pretty sure its just some amateurish error, but I would love for someone to demonstrate that.
Thanks for your time, hopefully my writing is legible enough, I haven't slept in a good 30 hours, which is probably why I'm having problems in the first place.
Answer by Steveo22 · Nov 18, 2016 at 08:42 AM
As a side note, this does not happen in the 3d engine.
Your answer
Follow this Question
Related Questions
Particle Effect to disable after it's played once 1 Answer
Particles not showing 0 Answers
Particle system does not follow emitter movement in world space: URP 1 Answer
OnParticleCollision() on parent script, for a child particle system? 2 Answers
How can you create non circular rocket flame particle effect? 0 Answers