Turn on and off ParticleSystem and LineRenderer on same object at the same time
I have an object that has a LineRenderer and a ParticleSystem on it and its a child of another object. I am trying to turn the renderer off for both so they are not visible till they are turned on. Here is what I'm using: private Renderer objectParticles; void Start () { objectParticles = gameObject.transform.Find("spawnCast").GetComponent(); objectParticles.enable = false; }
The problem with this is it seems to only turns off the LineRenderer on the child object and not the ParticleSystem Renderer too.
Answer by Fredex8 · Mar 12, 2016 at 06:37 PM
Without seeing how the gameObject is set up in Unity it is hard to know what you are actually doing there since you are finding a child by name and then using GetComponent without a type specified. This is probably a better solution:
private Transform childObject;
private ParticleSystem objectParticles;
private LineRenderer objectLineRenderer;
void Start()
{
childObject = gameObject.transform.Find("spawnCast");
objectParticles = childObject.GetComponent<ParticleSystem>();
objectLineRenderer = childObject.GetComponent<LineRenderer>();
objectParticles.enable = false;
objectLineRenderer.enable = false;
}
Without seeing what 'Spawn Cast' is I don't know how you have set it up but that should work. You might want to assign the child object via a different method though as find is pretty slow. Also you could use objectParticles.Play();
and .Stop();
instead of enabling and disabling it which may work better for you in the long run.