Get ParticleSystem (Shuriken) to play from array of game objects C#


I have 3 particle systems that are labeled with the tag Warp1. The 3 particle systems are placed into an empty GameObject in the hierarchy for organization.

I then load them into an array with the following code.

    private GameObject[] WarpIns;
    private void OnTriggerEnter(Collider collider)
            //section 0 checks
            if( == "S0G1open")
                Debug.Log ("hit gate 0 opener");
                //sectionState = SectionState.Section1; 
                //sectionWall = GameObject.Find("Section0G1");           
            if( == "S0G1close")
                Debug.Log ("hit gate 0 closer");
                WarpIns = GameObject.FindGameObjectsWithTag("Warp1");
        	wp = "path0";

private void selectWarpIn()
		int rndIndex = Random.Range(0, WarpIns.Length);
		Vector3 rndPosition =;
		destWarp = WarpIns[rndIndex].transform.position + rndPosition;
		//ParticleSystem myParticle = WarpIns[rndIndex].GetComponentInChildren();
		//ParticleSystem myParticle = WarpIns[rndIndex].GetComponent(ParticleSystem);
		//ParticleSystem myParticle = WarpIns[rndIndex].GetComponentInChildren(ParticleSystem);
		GameObject myParticle = WarpIns[rndIndex];

		if (myParticle == null) 
			Debug.Log("particle is empty");

			Debug.Log("particle has something " + myParticle);

I get this error:
NullReferenceException: Object reference not set to an instance of an object
UnityEngine.ParticleSystem.Play (Boolean withChildren) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/ParticleSystemBindings.cs:716)
UpdatedCityScript.selectWarpIn () (at Assets/Scripts/C# Scripts/UpdatedCityScript.cs:331)
UpdatedCityScript.Create (UnityEngine.GameObject enemy, System.String tagOfPathPoints) (at Assets/Scripts/C# Scripts/UpdatedCityScript.cs:302)
UpdatedCityScript+c__Iterator1.MoveNext () (at Assets/Scripts/C# Scripts/UpdatedCityScript.cs:339)
UpdatedCityScript:OnTriggerEnter(Collider) (at Assets/Scripts/C# Scripts/UpdatedCityScript.cs:98)


I have done that, I can get its transform, position, etc. I somehow cannot drill down to the particle.

->Particle system.


Line number 17 is the code that populates the array.

Thank you for your help.

Ah, yeah, sorry, I’d missed the FindGameObjectsWithTag call. Right, basically, the issue is that MonoBehaviour’s particleSystem reference only refers to a ParticleSystem component on the GameObject it is attached to, while in your hierarchy the systems are children, as you correctly identified and wrote those commented out GetComponentInChildren calls. GetComponentInChildren would only get your first component it finds, so the one you are after is GetComponentsInChildren.

GameObject myParticle = WarpIns[rndIndex];

if (myParticle != null) 
	ParticleSystem[] particleSystems = (ParticleSystem[]) myParticle.GetComponentsInChildren<ParticleSystem>();
	if (particleSystems != null && particleSystems.Length > 0)
		for (int i = 0; i < particleSystems.Length; i++)
  •  }*
  • }*
    From your hierarchy, this would get both the ParticleSystem components under the WarpIn object selected and play them. Does this do what you want?