• 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 LightWell · Jun 10, 2017 at 11:49 PM · objectdestroyparentchildnull reference exception

Unparenting and then deleting a child part of a prefab, SOMETIMES child throws null reference exception?

I've got a bullet being trailed with particles, so when it hits something bullet gets deleted, and I want to wait for the particles to disappear naturally before deleting the particle system. So I unparent the particle system, delete the bullet, then attempt to delete the particle system. Sometimes the particle system (the child in this case) doesn't get found when I fire two bullets in a specific situation (one bullet hits a target, the other is naturally destroyed when it's off screen, both are deleted using the bullets public delete function.) When the bullet hits the target, the target also calls the delete function on the bullet. Is unity handling children of the game object in a way I'm not expecting?

Some code is definitely ragged as I was trying to speed run making a game as fast as possible, the bullet script isn't really out of the ordinary. Hoping this is just me overlooking something simple. Bullet Code:

 public class bullet : MonoBehaviour {
     public float bulletSpeed = 1;
     public float bulletDamage = 1;
     // Use this for initialization
     void Start () {
         //gameObject.hideFlags = HideFlags.HideInHierarchy;
     }
     
     // Update is called once per frame
     void Update () {
         transform.Translate(Vector3.left * Time.deltaTime * bulletSpeed);
         if(!GetComponent<Renderer>().isVisible)
         {
             Invoke("delete", 2);
         }
     }
     
     public void delete()
     {
         transform.FindChild("Particle System").GetComponent<bulletSmokeFade>().waitASec();
         transform.DetachChildren();
         GameObject.Destroy(gameObject);
     }
 }

bulletSmokeFade code:

 public class bulletSmokeFade : MonoBehaviour {
 
     // Use this for initialization
     
     // Update is called once per frame
     public void waitASec () {
         GetComponent<ParticleSystem>().Stop();
         Invoke("cleanUp", 0.25f);
     }
 
     void cleanUp()
     {
         Destroy(gameObject);
     }
 }
 

The situation where the exception occurs:alt text

higher quality MP4: https://i.gyazo.com/35f0a2d91941403f94a4ef62d60f23bd.mp4

Comment
Add comment · Show 3
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 Dibbie · Jun 11, 2017 at 11:54 AM 1
Share

I think the problem is because your running this in Update - which is called every frame, so while on the first frame it actually runs your delete 2 seconds later, it remains unrendered, and will continue to call stacks of that function every 2 seconds, and the second or 5th time or whenever it actually happens, the object is already deleted or in the process of being deleted from a previous call of that function

The best thing to do if you want to keep it in Update, is also use IsInvoking.

 if(!IsInvoking("cleanUp")){Invoke("cleanUp", 0.25f);}


for example.

avatar image ShadyProductions Dibbie · Jun 11, 2017 at 01:04 PM 0
Share

Or add a condition check like so:

 if (gameObject != null)
          Destroy(gameObject);
avatar image Bunny83 ShadyProductions · Jun 11, 2017 at 01:58 PM 1
Share

The error is not the destroy line but this one:

 transform.FindChild("Particle System").GetComponent<bulletSmokeFade>().waitASec();

When FindChild returns null GetComponent can't be executed.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FiveFingerStudios · Oct 03, 2017 at 10:50 AM

no need to have the bulllet call waitasec.

Just have the particle delete itself after it's done playing.

Get the duration and use a coroutine with a delay to destroy it.

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

Do Child Objects Trigger Parent Objects? 1 Answer

Make gameobject a child without affecting scale? 1 Answer

Object vanishes when becoming a child 2 Answers

child object to parent c# 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