After player dies then respawns this error comes up on the console, MissingReferenceException: The object of type 'ResetOnRespawn' has been destroyed but you are still trying to access it?
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[i].gameObject.SetActive(true);
objectsToReset[i].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;
}
}
}
I'm thinking something right here is not correct it's saying its destroying something but theres nothing here to tell it to be destroyed thats why i'm also confused unless its borrowing a destroy function from another script somehow?
objectsToReset[i].gameObject.SetActive(true);
objectsToReset[i].ResetObject();
Answer by DanowebStudios · Oct 30, 2016 at 06:31 AM
Hello!
Posting this answer after speaking with @KUFgoddess for others in case they run into the same issue.
The problem here was the array "ObjectsToRespawn" is outdated when the call to "SetActive(true);" is made on line 84.
The array is populated in the Start Method, however as the games progresses, the objects in the array maybe destroyed by other scripts. Therefore resulting in this error.
The simplest fix would be to re-populate the list every time the co-routine is called before iterating (looping) through the array.
The suggested fix then, is to move:
objectsToReset = FindObjectsOfType<ResetOnRespawn>();
inside of the
public IEnumerator RespawnCo() //This is a corroutine//
{
function.
Hope this helps!
This worked perfectly and you helped me understand what's going on thank you!
Your answer
Follow this Question
Related Questions
Player walks a little bit into wall then gets pushed out again 0 Answers
How do I rotate an object around around another, following mouse position and facing it? 0 Answers
How to fix spite character changing position when turning? 1 Answer
Does anyone know how to make an object(enemy) follow a path set by the player in a 2D side scroller? 1 Answer