x


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); 
    }
}
more ▼

asked Apr 15 '12 at 11:19 PM

C-Blunt gravatar image

C-Blunt
22 5 8 9

Is the platform's movement being controlled by another script? Are there any other scripts that are using the platform?

Apr 15 '12 at 11:23 PM Lttldude

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.)

Apr 16 '12 at 03:14 AM Owen Reynolds

@owen-reynolds

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.

Apr 23 '12 at 09:23 AM Kryptos
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Apr 22 '12 at 09:38 PM

C-Blunt gravatar image

C-Blunt
22 5 8 9

(comments are locked)
10|3000 characters needed characters left

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);    
   }
}
more ▼

answered Apr 15 '12 at 11:24 PM

FizzyBear gravatar image

FizzyBear
67 4 10 12

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2495
x764
x195
x30

asked: Apr 15 '12 at 11:19 PM

Seen: 1365 times

Last Updated: Apr 23 '12 at 09:23 AM