- Home /
Get ParticleSystem (Shuriken) to play from array of game objects C#
HI,
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(collider.gameObject.name == "S0G1open")
{
Debug.Log ("hit gate 0 opener");
//sectionState = SectionState.Section1;
openGates(0);
//sectionWall = GameObject.Find("Section0G1");
}
if(collider.gameObject.name == "S0G1close")
{
Debug.Log ("hit gate 0 closer");
closeGates(0);
WarpIns = GameObject.FindGameObjectsWithTag("Warp1");
wp = "path0";
StartCoroutine(createEnemey(5));
}
}
private void selectWarpIn()
{
int rndIndex = Random.Range(0, WarpIns.Length);
Vector3 rndPosition = Vector3.zero;
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");
}
else
{
Debug.Log("particle has something " + myParticle);
myParticle.particleSystem.Play(true);
}
}
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) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) UpdatedCityScript:OnTriggerEnter(Collider) (at Assets/Scripts/C# Scripts/UpdatedCityScript.cs:98)
Hm, yes, indeed it is tied to that, you may be checking whether myParticle, ie. the GameObject, is null but you are not checking if particleSystem is null, ie. whether the object actually has that component.
I would put a breakpoint in there and check the contents of the array, to ensure that the contents are indeed what you expect. If you are still having issues, post the code actually populating that array along with a list of the actual hierarchy of those objects, I'm sure we'll be able to help!
Answer by Coreldavid · Feb 07, 2014 at 03:22 AM
HI,
I have done that, I can get its transform, position, etc. I somehow cannot drill down to the particle.
GameObject ->Particle system.
WarpPoint ->WarpParticle1.
Line number 17 is the code that populates the array. alt text
Thank you for your help.
Answer by AlkisFortuneFish · Feb 07, 2014 at 11:31 AM
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++)
{
particleSystems[i].Play();
}
}
}
From your hierarchy, this would get both the ParticleSystem components under the WarpIn object selected and play them. Does this do what you want?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
how to control particles along a path? 0 Answers