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 AlexPilk · Jan 08, 2014 at 01:07 PM · gameobjectdestroyinstance

What is wrong with this script?

Hi, guys. I was watching a tutorial, where it's been said that to destroy an instance of a prefab (which is instantiated in another script named ProjectileScript.js) 5 seconds after it's hit something you need to attach this script to your prefab:

 #pragma strict
 
 function Start () {
 
 }
 
 function Update () {
 
 }
   
 function OnCollisionEnter (collision : Collision){
 Destroy(gameObject,5);
 }

But it has returned this 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.

So after a lot of googling I wrote/copypasted this script:

 #pragma strict
 
 var targetScript;
 var targetObj: Transform; 
 
 function Start () {
 
 }
 
 function Update () {
 
 }
   
 function OnCollisionEnter (collision : Collision){
 var targetScript: ProjectileScript = targetObj.GetComponent(ProjectileScript);
 Destroy(targetScript.instance,5);
 print(collision.transform.name);
 }

The question is: was the first script supposed to work and destroy only one instance, but not the whole object, or maybe it was possible only in previous versions of Unity?

Thanks for help!

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
0
Best Answer

Answer by sath · Jan 08, 2014 at 02:33 PM

Your first script if is attached to your prefab (which you want to destroy) is working only at the time you instatiate the prefab from your ProjectileScript. The error you have means that the script on prefab (the one you are showing us) is working perfect but the other script (ProjectileScript : you are not showing us) is trying to do something with this prefab (which is destroyed). Find what is that or give us and the other script so we can have a better "look" to can help you..

Comment
Add comment · Show 3 · 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 AlexPilk · Jan 08, 2014 at 03:09 PM 0
Share

Thanks for response! Here's the script:

 #pragma strict
 
 var bullet : GameObject;
 var fireRate : float = 0.5;
 var timeTracker : float = 1;
 function Start () {
 
 }
 var instance : GameObject;
 
 function Update () {
 timeTracker+=1*Time.deltaTime;
 if(Input.GetButton("Fire1") && timeTracker>fireRate){
 timeTracker=0;
 instance = Instantiate(bullet, transform.position, transform.rotation);
 instance.rigidbody.velocity = transform.TransformDirection(Vector3(0,0,20));
 }
 
 }
 

Looks like it did not work correctly because ins$$anonymous$$d of dragging the prefab into the "bullet" object I dragged the prefab into Hierarchy view, and from there into the "bullet" object. So that means that ins$$anonymous$$d of using the prefab I used it's instance (and then instantiated instances of an instance), am I right?

avatar image sath · Jan 08, 2014 at 08:04 PM 0
Share

exactly !!!

but better make your script like this:

 #pragma strict
  
 var bullet : GameObject;//assign the prefab from Assets folder not from Hierarchy
 var fireRate : float = 0.5;
 var timeTracker : float = 1;
 private var instance : GameObject;//hide from Inspector
 
  
 function Update () {
     timeTracker+=1*Time.deltaTime;
     if(Input.GetButton("Fire1") && timeTracker>fireRate){
         timeTracker=0;
         instance = Instantiate(bullet, transform.position, transform.rotation);
         if(instance){//if instance really exist (it is not null)
             instance.rigidbody.velocity = transform.TransformDirection(Vector3(0,0,20));
         }
     }
  
 }
avatar image AlexPilk · Jan 08, 2014 at 08:06 PM 0
Share

Thank you :)

avatar image
0

Answer by Jessy · Jan 08, 2014 at 01:39 PM

These

 Destroy(gameObject,5);
 Destroy(targetScript.instance,5);


are only equivalent if the Game Object that the script is attached to is also the Game Object that targetScript.instance is attached to.

If you destroy a Game Object, you can't reference it anymore.

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 AlexPilk · Jan 08, 2014 at 01:44 PM 0
Share

Thanks, but the question is why am I getting this error, and is there a more simple solution to this problem?

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

19 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

destroying game object in a specific transform 2 Answers

Alternatives to Instantiate/Destroy 1 Answer

Destroy a gameobject instance but also save it in a new variable 1 Answer

Instantiated GameObject being deleted when destroying original 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