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 Ebil · Apr 04, 2014 at 08:18 PM · getcomponentparticle systemexception

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?

Comment
Add comment · Show 7
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 robertbu · Apr 04, 2014 at 09:08 PM 1
Share

Are you sure that 'i' is less than transform.childCount? Note that you can do

  transform.GetComponent<ParticleSystem>()
avatar image Ebil · Apr 04, 2014 at 09:16 PM 0
Share

ohn damnit I forgot the .transform

 engines[0].transform.GetChild(i).transform.GetComponent<ParticleSystem>() == null

works ... Thanks. Please post this as answere ^^

avatar image ivomarel · Apr 04, 2014 at 09:24 PM 0
Share

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.

avatar image Ebil · Apr 04, 2014 at 09:33 PM 0
Share

Well I edited my code while that but I dont know what else could have solved this now ~_~

avatar image Gruffy · Apr 04, 2014 at 10:29 PM 0
Share

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

avatar image Ebil Gruffy · Apr 05, 2014 at 08:38 AM 0
Share

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 ;)

avatar image Gruffy Gruffy · Apr 05, 2014 at 08:43 AM 0
Share

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

0 Replies

· Add your reply
  • Sort: 

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

23 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 avatar image avatar image avatar image

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


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