- Home /
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.
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.
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.
The disable-move-enable approach is the better way to do this.
It will also be faster too.
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().