Im making a game where you unlock doors with keys and its been working fine all day, however i have just opened Unity and in the console it says this:
NullReferenceException: Object reference not set to an instance of an object Door.OnTriggerStay2D (UnityEngine.Collider2D collision) (at Assets/Door.cs:25)
help?
Key.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Key : MonoBehaviour{
private SpringJoint2D spring;
// Start is called before the first frame update
void Start(){
spring = GetComponent<SpringJoint2D>();
GameObject backpack = GameObject.FindWithTag("Backpack");
spring.connectedBody = backpack.GetComponent<Rigidbody2D>();
spring.enabled = false;
}
private void OnTriggerEnter2D(Collider2D collision) {
if(collision.gameObject.tag == "Player" && !spring.enabled){
spring.enabled = true;
}
}
}
Door.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Door : MonoBehaviour{
public GameObject connectedDoor;
public bool teleported = false;
private GameObject player;
// Start is called before the first frame update
void Start(){
player = GameObject.FindWithTag("Player");
}
// Update is called once per frame
void Update(){
if(teleported && Input.GetAxisRaw("Vertical") < 1){
teleported = false;
}
}
private void OnTriggerStay2D(Collider2D collision) {
if(collision.gameObject.tag == "Key" && Input.GetAxisRaw("Vertical") == 1) {
if(Input.GetAxisRaw("Vertical") == 1 && !teleported){
player.transform.position = connectedDoor.transform.position;
connectedDoor.GetComponent<Door>().teleported = true;
}
}
}
}