I’m fairly new to this, so I apologize in advance. I’m making a maze with keys to unlock the exit. The level is working, and I’m trying to get it to reload the scene when the goal is reached (key is collected and exit reached). that part is working ok, but when it reloads the scene, the new instance is not loading in the key generator. it loads the goal(the door) but it is not generating/ loading the keys to unlock it. I keep getting a Missing Reference Exception, saying the object has been destroyed. The error shows up on 2 lines code (i marked them with comments):
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
public class MazeDirectives : MonoBehaviour
{
public int keysToFind;
public Text keysValueText;
public MazeGoal mazeGoalPrefab;
public MazeKey mazeKeyPrefab;
MazeGoal mazeGoal;
int foundKeys;
List<Vector3> mazeKeyPositions;
void Awake()
{
MazeGenerator.OnMazeReady += StartDirectives;
}
void Start()
{
SetKeyValueText();
}
void StartDirectives()
{
mazeGoal = Instantiate(mazeGoalPrefab, MazeGenerator.instance.mazeGoalPosition, Quaternion.identity) as MazeGoal;
**mazeGoal.transform.SetParent(transform);** //this line brings the error
mazeKeyPositions = MazeGenerator.instance.GetRandomFloorPositions(keysToFind);
for (int i = 0; i < mazeKeyPositions.Count; i++)
{
MazeKey mazeKey = Instantiate(mazeKeyPrefab, mazeKeyPositions*, Quaternion.identity) as MazeKey;*
mazeKey.transform.SetParent(transform);
}
}
public void OnGoalReached()
{
Debug.Log(“Goal Reached”);
if (foundKeys == keysToFind)
{
Debug.Log(“Escape the maze”);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); //this line also
}
}
public void OnKeyFound()
{
foundKeys++;
SetKeyValueText();
if (foundKeys == keysToFind)
{
GetComponentInChildren().OpenGoal();
}
}
void SetKeyValueText()
{
keysValueText.text = foundKeys.ToString() + " of " + keysToFind.ToString();
}
}
,