Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Coreldavid · Feb 06, 2014 at 10:07 PM · c#particlesarrays

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)

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Coreldavid · Feb 06, 2014 at 09:46 PM 0
Share

I realize it is tied to the

  .Play(true);
avatar image AlkisFortuneFish · Feb 07, 2014 at 12:44 AM 0
Share

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!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.


warppointissue.jpg (53.2 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0
Wiki

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Coreldavid · Feb 12, 2014 at 10:04 PM 0
Share

AlkisFortuneFish Thanks, This works perfectly!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

how to control particles along a path? 0 Answers

Application.LoadLevel and particle system 1 Answer

Obtaining the Variable of a Prefab in an array. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges