Okay so I’m going to try to describe my issue, but first some context. My game is about getting to the finish before a spike wall kills you. When you die the spike wall should go back to its original place, as should the player. But when the wall kills you the spike wall does not respawn because of the error “NullReferenceException: Object reference not set to an instance of an object”
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelManager : MonoBehaviour {
public GameObject deathParticle;
public GameObject respawnParticle;
public GameObject currentCheckpoint;
public float respawnDelay;
public GameObject playerPrefab;
private PlayerMovement player;
private WallFailureScript wall;
void Start () {
player = FindObjectOfType<PlayerMovement>();
wall = FindObjectOfType<WallFailureScript>();
}
void Update () {
}
public void RespawnPlayer() {
StartCoroutine("RespawnPlayerCo");
StartCoroutine("RespawnWall");
}
public IEnumerator RespawnPlayerCo() {
Instantiate(deathParticle, player.transform.position, player.transform.rotation);
playerPrefab.SetActive(false);
player.GetComponent<Renderer>().enabled = false;
yield return new WaitForSeconds(respawnDelay);
player.transform.position = currentCheckpoint.transform.position;
playerPrefab.SetActive(true);
player.GetComponent<Renderer>().enabled = true;
Instantiate(respawnParticle, currentCheckpoint.transform.position, currentCheckpoint.transform.rotation);
}
public IEnumerator RespawnWall() {
yield return new WaitForSeconds(respawnDelay);
wall.transform.position = currentCheckpoint.transform.position;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;
public class WallFailureScript : MonoBehaviour {
public static int moveSpeed = 1;
public Vector3 cpuDirection = Vector3.right;
public LevelManager levelManager;
void Start() {
levelManager = FindObjectOfType<LevelManager>();
}
void OnTriggerEnter2D(Collider2D collision){
if (collision.name == "Player")
levelManager.RespawnPlayer();
}
void FixedUpdate(){
transform.Translate(cpuDirection * moveSpeed * Time.deltaTime);
}
}