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
1
Question by gitoffame · Jul 20, 2014 at 01:26 PM · instantiatedestroyreferenceclonemissingreferenceexception

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

Hello... I'm trying to set up a teleport system that destroys the original object and then spawns a clone of that object at the exit portal.

I don't want to just instantiate a prefab, as I want to keep changes to the object that happen during gameplay, and I don't want to simply move the object, because I have a spawner object that has a few useful functions like a delay and clearing the area of obstructions, etc.

I want to pass the object in its current state as a variable, destroy the original, then have the spawner instantiate a clone after a delay.

However, I'm getting the 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."

Here is the code for my warp entrance object:

 #pragma strict
 
 var on : boolean = true;
 var teleportTarget : GameObject;
 var ballSpawn : GameObject;
 var yieldBefore : float = 00;
 var yieldAfter : float = 00;
 var preserveVelocity : boolean = true;
 var holdDuringDelay : boolean = true;
 var affectTag : String = "Ball";
 private var teleporting : boolean = false;
 private var teleportedObject : GameObject;
 private var initialPosition : Vector3;
 private var incomingVelocity : Vector3;
 
 function Start ()
     {
     
     }
 
 function Update ()
     {
     if (teleporting && holdDuringDelay)
         {
         teleportedObject.transform.position = initialPosition;
         }
     }
 
 function OnTriggerEnter(objectCollision : Collider)
     {
     if ((objectCollision.gameObject.tag == affectTag) && (teleportedObject != objectCollision.gameObject) && (on))
         {
         initialPosition = objectCollision.gameObject.transform.position;
         incomingVelocity = objectCollision.gameObject.rigidbody.velocity;
         teleport(objectCollision.gameObject);
         }
     }
     
 function teleport(object : GameObject)
     {
         teleporting = true;
         teleportedObject = object;
         yield WaitForSeconds(yieldBefore);
         var instantiatedObject = Instantiate(ballSpawn, teleportTarget.transform.position, teleportTarget.transform.rotation);
         if (instantiatedObject.GetComponentInChildren(BallSpawn))
             {
             instantiatedObject.GetComponentInChildren(BallSpawn).spawnOnStart = true;
             instantiatedObject.GetComponentInChildren(BallSpawn).ball = teleportedObject;
             instantiatedObject.GetComponentInChildren(BallSpawn).spawnDelay = this.yieldAfter;
             instantiatedObject.GetComponentInChildren(BallSpawn).destroyAfterSpawn = true;
             if (preserveVelocity)
                 {
                 instantiatedObject.GetComponentInChildren(BallSpawn).spawnWithVelocity = this.incomingVelocity;
                 }
             }
         teleportedObject = null;
         teleporting = false;
         DestroyOriginal(object);
     }
     
 function DestroyOriginal(object : GameObject)
     {
     Destroy(object);
     }

Thanks in advance, smart people.

Comment
Add comment · Show 4
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 · Jul 20, 2014 at 06:04 PM 1
Share

So what line is generating the error? Any chance you have a Destroy() call in the original object? Any chance that something else is referencing something in the original that is not getting updated. Or is your original referencing any children? Your question says:

I want to pass the object in its current state as a variable, destroy the original, then have the spawner instantiate a clone after a delay.

While it does not appear your code is doing it this way, you cannot destory before you instantiated the new one. The order needs to be: 1) hide original, 2) wait, 3) instantiate the new one, 4) destory the original.

avatar image gitoffame · Jul 21, 2014 at 01:49 AM 0
Share

Hmm... Is there no way to store it as a new class or something? I suppose if there's no way, then I can rewrite my spawner to use a hide, move, unhide approach, but I thought it would be cleaner if I could just store the thing in memory somewhere until I needed it again... Thanks, though.

avatar image Kiwasi · Jul 21, 2014 at 02:30 AM 1
Share

The disable-move-enable approach is the better way to do this.

It will also be faster too.

avatar image robertbu · Jul 21, 2014 at 03:47 AM 1
Share

If you really want to destroy the original, you could order it this way:

1) create the new one, 2) disable the new one, 3) destroy the old one, 4) wait 5) enable the new one.

What you cannot do is destroy the old one and then later try to make a copy of the destroyed one using Instantiate().

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by gitoffame · Jul 22, 2014 at 04:22 PM

Thanks for clearing this up.

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.

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

Destroy and rebuild game objects from lists 1 Answer

restricting clone number 2 Answers

How can I destroy my Instance without renaming it? 2 Answers

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

Reference an Instance of an Object?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