Question by
jackdeazley · Dec 10, 2018 at 08:07 PM ·
c#collisiondestroy
Game Objects wont appear after restart
I am making a maze game and the keys which are needed to be collected to complete it wont appear again if the game restarts, I get the following error;
MissingReferenceException: The object of type 'MazeDirectives' 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.
I am just disabling the MazeKey object, not destroying it, can anyone help? Below is my code;
MazeKey.cs
using UnityEngine;
using System.Collections;
public class MazeKey : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D other)
{
transform.parent.SendMessage("OnKeyFound", SendMessageOptions.DontRequireReceiver);
gameObject.SetActive(false);
}
}
MazeDirectives.cs
mazeKeyPositions = MazeGenerator.instance.GetRandomFloorPositions(keysToFind);
for (int i = 0; i < mazeKeyPositions.Count; i++)
{
MazeKey mazeKey = Instantiate(mazeKeyPrefab, mazeKeyPositions[i], Quaternion.identity) as MazeKey;
mazeKey.transform.SetParent(transform);
}
}
Comment
Your answer
Follow this Question
Related Questions
creating a Breakout game in unity3d c#-bricks wont get destroyed 0 Answers
how do i collide and kill the enemy while pressing space Unity 5 C# 2 Answers
Player falls through destroy collider 1 Answer
Unity 5: AddForce Increases power when already being pushed towards a collider. How to make stop? 1 Answer