Question by
PGAudio · Feb 02, 2018 at 12:17 PM ·
instantiateparticlesystemnull
Instantiate ParticleSystem prefab as ParticleSystem returns null. As GameObject returns the prefab
Hi Guys...
Im trying to instantiate a particle system prefab I made (PSTest) and it's returning null.. GameObject TempParticle = Instantiate (Resources.Load("PSTest"), new Vector3 (-2.5f, -2, -5), Quaternion.identity) as GameObject; Debug.Log (TempParticle);
This is returning the correct object.
But if I use:
ParticleSystem TempParticle = Instantiate (Resources.Load("PSTest"), new Vector3 (-2.5f, -2, -5), Quaternion.identity) as ParticleSystem; Debug.Log (TempParticle);
It returns null.
Any Idea what's happening ??
Comment
Best Answer
Answer by Nighfox · Feb 02, 2018 at 02:14 PM
That's because you're trying to convert/cast a GameObject
into a Component
. Instead, you should get the object first, then get the component afterwards:
GameObject go = Instantiate(spawnObject, transform.position, Quaternion.identity, null) as GameObject;
ps = go.GetComponent<ParticleSystem>();
Your answer
![](https://koobas.hobune.stream/wayback/20220612151803im_/https://answers.unity.com/themes/thub/images/avi.jpg)