|
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);
}
}
(comments are locked)
|
|
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.
(comments are locked)
|
|
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..
(comments are locked)
|

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 withPpointing to nothing (Unity automatically nulls P for you.)@owen-reynolds
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.