Why is my destroy platform script now working? NullReferenceException: Object reference not set to an instance of an object.
using UnityEngine;
using System.Collections;
public class PlatformDestroyer : MonoBehaviour {
public GameObject platformDestructionPoint;
// Use this for initialization
void Start () {
platformDestructionPoint = GameObject.Find ("PlatformDestructionPoint");
}
// Update is called once per frame
void Update () {
if(transform.position.x < platformDestructionPoint.transform.position.x)
{
Destroy (gameObject);
}
}
}
Hello, I am a complete beginner to Unity and am trying to learn by following a 2d endless runner tutorial. So far it has been well, but when trying to create a script that destroys platforms after my character passes by I keep getting an error message. It has something to do with line 16 of my code and I have no idea what it means or how to fix it. Please help me to fix my problem. Thank you!
Answer by jgodfrey · Apr 12, 2016 at 01:12 AM
I'd guess your "platformDestructionPoint" reference is null. If that's the case, then this code in Start() isn't finding the specified object:
platformDestructionPoint = GameObject.Find ("PlatformDestructionPoint");
Do you really have an object named "PlatformDestructionPoint" in the game? Is the spelling (and character case) exactly correct?
Thank you very much! I seemed to have misspelled "PlatformDestructionPoint" in my game.