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 Stak_Overflo669 · Aug 07, 2013 at 01:50 AM · prefabdestroymissingreferenceexception

MissingReferenceException after destroying a prefab?

I'm sure this gets asked all the time but I can't seem to find an answer that works. So, brand new to Unity and gave development in general.

I've created a generic GameObject to act as a spawn point when the user left clicks for a projectile prefab that gets destroyed after 5 seconds. This works, except after 5 seconds if I try to fire to again I get a MissingReferenceException saying The object of type 'GameObject' has been destroyed but you are still trying to access it.

Somehow, it's removing that same projectile prefab from my GameObject that spawns them.

Recap: -"Empty" GameObject with a script attached that spawns a Projectile prefab on mouse up -Prefab has a script attached that destroys a designated gameObject -After the prefab is destroyed, my mouse up no longer works and throws a MissingeReferenceException

Script #1 (attached to part of the player's model):

 var power: float = 1000;
 var bulletPrefab: GameObject;

 function Update () {
     if (Input.GetMouseButtonUp(0)){
         var pos: Vector3 = transform.position;
 
         bulletPrefab = Instantiate(bulletPrefab, transform.position, transform.rotation);
         bulletPrefab.rigidbody.AddForce(Vector3.forward * power);
         audio.Play();
     }
 }

Script #2 (attached to prefab):

 var ttl = 5;
 var thingToDestroy: GameObject;

 function Awake() {
     Destroy(thingToDestroy,ttl);
 }

In both scripts, the GameObject variable is the same prefab and I can only assume this is my problem... But I have no idea what I should be doing?

Also, in script #2, if I try simply Destroy(this, ttl) it doesn't destroy the object the script is attached to?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Aug 07, 2013 at 02:24 AM

Well, your bulletPrefab variable used to hold a reference to a prefab. When you instantiate the prefab you get a prefab instance but you overwrite your bulletPrefab variable with the reference to the instance, so the reference to the prefab is lost. When the one instance you have created gets destroyed, bulletPrefab will become null.

You should use a local temp-variable like this:

 var power: float = 1000;
 var bulletPrefab: GameObject;
  
 function Update () {
     if (Input.GetMouseButtonUp(0)){
         var clone : GameObject = Instantiate(bulletPrefab, transform.position, transform.rotation);
         clone.rigidbody.AddForce(Vector3.forward * power);
         audio.Play();
     }
 }
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 Eric5h5 · Aug 07, 2013 at 02:29 AM 0
Share

Good catch, I didn't see the bit about using the same variable.

avatar image
1

Answer by Eric5h5 · Aug 07, 2013 at 01:56 AM

Don't bother with script #2, just do

 Destroy (bulletPrefab, timeToLive);

in script #1.

Comment
Add comment · Show 2 · 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 Stak_Overflo669 · Aug 07, 2013 at 02:18 AM 0
Share

I had thought of that, but I'm getting the same issue :(

avatar image Eric5h5 · Aug 07, 2013 at 02:23 AM 0
Share

Are you actually using prefabs? Objects in the scene are instances, not prefabs.

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

16 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

Related Questions

Clone of rigid body prefab destruction if at certain position 3 Answers

Instantiated Prefab doesnt destroy 2 Answers

Pass a copy of a GameObject as variable to another script? 1 Answer

Cannot Destroy Instanitaed prefab. 1 Answer

Destroying Instantiated Prefab 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