- Home /
MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.
Hi, I'm making a doodle jump style game as my first unity project. The game begins and the platform boosts your jump speed and gets destroyed when you hit it which is fine but unity is returning the following error and no more platforms are spawned. From what I've read i need to set the destroyed platform to null, but im not sure how to do this...Please help!
"MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it."
using UnityEngine; using System.Collections;
public class Playermovement : MonoBehaviour { public float movementSpeed = 8.0f;
void Update() {
rigidbody.velocity = new Vector3(0, rigidbody.velocity.y, 0); //Set X and Z velocity to 0
transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * movementSpeed, 0, 0);
}
void Jump() //Increases velocity of player when called.
{
rigidbody.velocity = new Vector3(0, 0, 0);
rigidbody.AddForce(new Vector3(0, 700, 0), ForceMode.Force);
}
void OnCollisionEnter(Collision collision){
if(collision.gameObject.tag == "platform")
{
Jump ();
Destroy(collision.gameObject);
}
}
Is the platform's movement being controlled by another script? Are there any other scripts that are using the platform?
What line of what script is the error on?
When you destroy the platform, any scripts on it also die, so no issues there. But, suppose someone else has Transform P;
which was set the that platform. After the player destroys it, that guy is left with P
pointing to nothing (Unity automatically nulls P for you.)
Unity automatically nulls P for you
Actually, no. Unity does not, and that's a case for memory leaks. But Unity does detect that the object was previously destroyed, hence the error message.
Answer by C-Blunt · Apr 22, 2012 at 09:38 PM
Solved it now, Owen Reynolds was correct so many thanks to you! My script for respawning platforms was still trying to access the deleted platform clones.
Answer by FizzyBear · Apr 15, 2012 at 11:24 PM
Why don't you create a clone prefab? Then access that in the code? Destroy the clone while keeping the main object..
You could use the following as an example..
var projectile : Transform;
var bulletSpeed : int = 100;
function Update ()
{
var fireBall = GameObject.Find("spawnPoint");
// Put this in your update function
if (Input.GetMouseButtonDown(0)) {
// Instantiate the projectile at the position and rotation of this transform
var clone : Transform;
clone = Instantiate(projectile, fireBall.transform.position, transform.rotation);
// Add force to the cloned object in the object's forward direction
clone.rigidbody.AddForce(clone.transform.forward * bulletSpeed);
Destroy(clone.gameObject, 1);
}
}