- Home /
nullreferenceexception object reference not set to an instance of an object
using UnityEngine;
using System.Collections;
public class PlayerSpawn : MonoBehaviour
{
public Transform playerSpawn;
public Vector2 currentTrackPosition;
public bool activeRespawnTimer = false;
public float respawnTimer = 1.0f;
public float resetRespawnTimer = 1.0f;
// Use this for initialization void Start () {
if(playerSpawn != null)
{
transform.position = playerSpawn.position;
Debug.Log(playerSpawn);
}
}
// Update is called once per frame
void Update ()
{
if(activeRespawnTimer)
{
respawnTimer -= Time.deltaTime;
}
if(respawnTimer <= 0.0f)
{
transform.position = currentTrackPosition;
respawnTimer = resetRespawnTimer;
activeRespawnTimer = false;
}
}
void OnTriggerEnter2D(Collider2D other) {
//im getting the error messege at this position
if(other.tag == "DeadZone")
{
activeRespawnTimer = true;
}
if(other.tag == "CheckPoint")
{
currentTrackPosition = transform.position;
}
} }
im getting this error in unity nullreferenceexception object reference not set to an instance of an object unity . im newbie. thank you for the help.
Answer by trololo · Mar 31, 2014 at 07:33 AM
currentTrackPosition need to be initialized (with new keyword), as playerSpawn might always be null.
Answer by alaarhouma · Mar 31, 2014 at 09:03 PM
void OnTriggerEnter2D(Collider2D other)
{
if(other != null && other.tag != null)
{
if(other.tag == "DeadZone")
{
activeRespawnTimer = true;
}
if(other.tag == "CheckPoint")
{
currentTrackPosition = transform.position;
}
}
}
i found the solution thank you!
Your answer
Follow this Question
Related Questions
colliders with touch 0 Answers
how i can optimize the following code: 2 Answers
Trying to drag an object with touch using Rigidbody2D.MovePosition 1 Answer
Show activity indicator in center of screen 0 Answers
ASDAsdASDasdqweqw3edqwdQWDEqwedQWD 3 Answers