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 CrucibleLab · Oct 16, 2012 at 06:06 AM · destroyreferencetankmount

Changing Meshes - MissingReferenceException

Hello folks,

I hit a snag while working on a Unity Scene that serves as a garage for tank units where players can pick and choose among three different types of meshes: 1) tank body, 2) turret, and 3) gun. In the scene, I have a 3D rendition of the tank along with buttons so that the player can click to cycle through the various tank part meshes. And, of course, the rendered tank gets a real-time visual update depending on the player selection. Things seem to be working just fine initially.

But, when the player chooses a different turret (meaning, the tank body and gun remain the same), I get the following error:

MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.

As for the structure of the tank unit itself, the 'tank body' gameobject has a 'turret mount' where the 'turret' gameobject is placed as a child of the 'tank body'. Subsequently, the 'turret' gameobject has a 'gun mount' where the 'gun' gameobject is placed as a child. So, the object hierarchy is two-level deep.

As for my code, it is not in a single file so I'd rather not just copy the whole thing here but let me do my best to give you what's happening in the background (I will be happy to add code snippets later if necessary):

1) I initialize each part (body, turret, gun) based on a set of default choice. Simplified code looks something like the following:

GameObject obj = Instantiate(gameobject, transform.position, Quaternion.identity) as GameObject;
obj.transform.parent = transform;
obj.transform.rotation = transform.rotation;

2) When I try to change the 'turret', what I do first is to look at the 'turret mount' and destroy all child objects so that only the new turret choice will be visible, not the default one:

if (turretmount.transform.childCount > 0)
     for (int cnt=0; cnt < turretmount.transform.childCount; cnt++)
          Destroy(turretmount.transform.GetChild(cnt).gameObject);

This destroys all child objects below the 'turret mount' so the 'turret' and the 'gun' gameobjects get destroyed.

At this point, I use the code routine I mentioned in Step #1 to re-instantiate the 'turret' gameobject (which the player just selected) and the same 'gun' gameobject that was previously used. It is at this point I run into the above error.

When I comment out the code part that re-instantiates the 'gun' gameobject, I get no error whatsoever. Switching from one turret to another works just fine. The error is thrown when I try to re-instantiate the 'gun' gameobject.

Lastly, I did quite a bit of research on the aforementioned error so I kind of understand why this error occurs (the GameObject is destroyed so it is now pointing to an object that is no longer there) but I had no luck with finding a fix for my problem.

Any tips/help/insights will be greatly appreciated!

Comment
Add comment · Show 1
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 Fattie · Oct 16, 2012 at 07:48 AM 0
Share

i don't really entirely understand your problem in detail but ...

(1) just add the new one, AND THEN remove the old one.

(2) try the age-old video game engineering trick of just $$anonymous$$OVING THINGS OFFSCREEN !

as a TEST, just $$anonymous$$OVE the old turret 5 meters to the right (change .localPosition), pause and have a look if that's working. now add the new turret (correct location). pause and check out the situation. now try to delete, disable, destroy, whatever the old one .. again check out the situation

hope helps

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

10 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

Related Questions

Destroy and rebuild game objects from lists 1 Answer

Destroying Prefab Also Destroys Prefab Reference 1 Answer

Losing references after reloading the scene, even though they exist again. 1 Answer

Rescaling of grid and tilemap causes it not to destruct properly 0 Answers

Pass a copy of a GameObject as variable to another script? 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