- Home /
Check if ParticleSystem exist
Somehow I dont get it to work, I always get a NullReferenceExeption (which I want to avoid of course) What I tried:
trailDown[i] = engines[0].transform.GetChild(i).gameObject.GetComponent<ParticleSystem>();
if(trailDown[i].particleSystem == null)
Debug.LogError("Particle System Component not found !");
also just trailDown[i] without .particleSystem
and
if(engines[0].transform.GetChild(i).particleSystem == null)
Debug.LogError("Particle System Component not found !");
And all combinations that I got into mind.
So how is this done right?
Are you sure that 'i' is less than transform.childCount? Note that you can do
transform.GetComponent<ParticleSystem>()
ohn damnit I forgot the .transform
engines[0].transform.GetChild(i).transform.GetComponent<ParticleSystem>() == null
works ... Thanks. Please post this as answere ^^
GetChild(i) already returns a transform, that wasn't the issue.
if (engines[0].transform.GetChild(i).particleSystem == null) { Debug.Log("Particle System is null"); }
should just work.
Well I edited my code while that but I dont know what else could have solved this now ~_~
Hey bud, I`m unsure if you have already checked the docs but here is the link to the ParticleSystem docs & The ParticleSystem.IsAlive() $$anonymous$$ethod of the ParticleSystem Class
And below is the code on that link showing you how you can access the Particle System to check it.
// Auto destruct script that can be added to the root particle system
// of a particle effect. It will destroy the gameobject and its children.
using UnityEngine;
public class AutoDestructParticleSystem : $$anonymous$$onoBehaviour
{
void LateUpdate ()
{
if (!particleSystem.IsAlive())
Object.Destroy (this.gameObject);
}
}
Obviously here, they are invoking the destroy method on the object if if the scoped ParticleSystem method returns false. But you get the picture and this is the method you should invoke to see if your particle system still exists.
With the above in $$anonymous$$d... your code may be better to read...
trailDown[i] = engines[0].transform.GetChild(i).gameObject.GetComponent<ParticleSystem>();
if(!trailDown[i].particleSystem.IsAlive())
Debug.LogError("Particle System Component not found !");
else{Debug.Log("Particle is alive" + trailDown[i].particleSystem.IsAlive().ToString())}
Sorry but this is untested, I wont be able to do that until later tonight..If you can and find it doent solve your issues then come on back here and we will carry on through it :) Take care bud. Gruffy
Not exactly what I want (maybe my question was a little unclear), I dont think you can check if Particle is alive if there is no ParticleSystem component attached. And that was what I wanted, to check if the Component is attached (and I can work with it). And the above comments (kinda) solved the problem. Thanks anyway ;)
Yeah sorry bud but question is worded a little vague as you have asked if particle system still exists, which unity accounts for, not if the particle system component still exists etc, which is obviously a part of the Component class etc. Sorry for any confusions there, I change to a comment. Take care bud. Gruffy
Your answer
Follow this Question
Related Questions
Best pattern for activating something 1 Answer
How to access variable from another c# script file? 1 Answer
GetComponent null reference C# 1 Answer
How to access a variable for a Component of a different GameObject ? 1 Answer
I am trying to use a variable in the parameter of gameObject.GetComponent("var's Name"). 1 Answer