This is my first post here, i was following a Tutorial on YOUTUBE from UNITY “Live Training 20 Jan 2014 - LMG: 2D Infinite Runner”
Everything works fine except that when my player is destroyed I get this error:
MissingReferenceException: The object of type ‘Transform’ 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.
UnityEngine.Transform.get_position () (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineTransform.cs:27)
CameraFollow.Update () (at Assets/Scripts/CameraFollow.cs:11)
here is my CameraFollow script:
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour {
public Transform player;
void Update ()
{
transform.position = new Vector3 (player.position.x + 3, 2, -10);
}
}
and here is my “Destroyer” Script:
using UnityEngine;
using System.Collections;
public class Destroyer : MonoBehaviour {
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player") { //checking if player entered destroyer
Application.LoadLevel (1);
return;
}
if (other.gameObject.transform.parent)
{
Destroy (other.gameObject.transform.parent.gameObject);
}
else
{
Destroy (other.gameObject);
}
}
}
If you are following the training video exactly than you used the 2d Character from the sample assets. However if you created your own character I would double check if your player game object is tagged with “Player”.
Basically if your player hits your destroyer and hence the script kicks off, then you should fall into the first If statement where you have other.tag == “Player”. Which should reload the level and then exit the function. So nothing should be destroyed. So your error is not from the player being destroyed, unless the tag is not correct.
Thank you so much! That was exactly it, I created my own character, but never tagged him as Player (didn’t know I had to). Now everything works and my first game is complete =D !!! Thank you again.