I’ve been looking at questions that are similar to my question but I havent had any luck. Ok I am following a unity course for a 2d platformer but it seems that the teacher is not active at the moment so I am stuck right here until I can understand what this error is fully saying. After player dies then respawns
I get this console messege
MissingReferenceException: The object of type ‘ResetOnRespawn’ 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.
LevelManager+c__Iterator0.MoveNext () (at Assets/MyScripts/LevelManager.cs:84)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
***Im really new / unexperienced so I would appreciate if things were explained in the simplest form possible. I would love to actually understand the mistake I made. ***
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class LevelManager : MonoBehaviour {
//ForPausingOnDeath / Respawn
public float waitToRespawn;
public PlayerController thePlayer;
public GameObject DeathExplosion;
//Coins
public int hatCount;
//UI
public Text hatText;
public Image Hat;
public Image Hat2;
public Image Hat3;
public Sprite hatFull;
public Sprite hatHalf;
public Sprite hatEmpty;
public int maxHealth;
public int healthCount;
private bool respawning;
//Array //[] square brackets mean you want to create an array
public ResetOnRespawn[] objectsToReset;
// Use this for initialization
void Start () {
thePlayer = FindObjectOfType<PlayerController>();
hatText.text = "hats:" + hatCount;
healthCount = maxHealth;
objectsToReset = FindObjectsOfType<ResetOnRespawn>();
}
// Update is called once per frame
void Update () {
if(healthCount <= 0 && !respawning)
{
Respawn();
respawning = true;
}
}
public void Respawn()
{
StartCoroutine("RespawnCo");
}
public IEnumerator RespawnCo() //This is a corroutine//
{
thePlayer.gameObject.SetActive(false);
Instantiate(DeathExplosion, thePlayer.transform.position, thePlayer.transform.rotation);
yield return new WaitForSeconds(waitToRespawn);
healthCount = maxHealth;
respawning = false;
UpdateHatMeter();
hatCount = 0;
hatText.text = "hats:" + hatCount;
thePlayer.transform.position = thePlayer.respawnPosition;
thePlayer.gameObject.SetActive(true);
for(int i = 0; i < objectsToReset.Length; i++)
{
objectsToReset*.gameObject.SetActive(true);*
objectsToReset*.ResetObject();*
}
}
public void AddHats(int HatsToAdd)
{
hatCount += HatsToAdd;
hatText.text = “hats:” + hatCount;
}
public void HurtPlayer(int damageToTake)
{
healthCount -= damageToTake;
UpdateHatMeter();
thePlayer.Knockback();
}
public void UpdateHatMeter()
{
switch(healthCount)
{
case 6:
Hat.sprite = hatFull;
Hat2.sprite = hatFull;
Hat3.sprite = hatFull;
return;
case 5:
Hat.sprite = hatFull;
Hat2.sprite = hatFull;
Hat3.sprite = hatHalf;
return;
case 4:
Hat.sprite = hatFull;
Hat2.sprite = hatFull;
Hat3.sprite = hatEmpty;
return;
case 3:
Hat.sprite = hatFull;
Hat2.sprite = hatHalf;
Hat3.sprite = hatEmpty;
return;
case 2:
Hat.sprite = hatFull;
Hat2.sprite = hatEmpty;
Hat3.sprite = hatEmpty;
return;
case 1:
Hat.sprite = hatHalf;
Hat2.sprite = hatEmpty;
Hat3.sprite = hatEmpty;
return;
case 0:
Hat.sprite = hatEmpty;
Hat2.sprite = hatEmpty;
Hat3.sprite = hatEmpty;
return;
default:
Hat.sprite = hatEmpty;
Hat2.sprite = hatEmpty;
Hat3.sprite = hatEmpty;
return;
}
}
}