objects destroyed when reloading scene,objects being destroyed when reloading scene

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();
}
}
,

Well hey, I am a noob too. So, let’s figure this out together. So, If you are doing a scene load and it’s not additive, you cannot use the game objects from the previous scene as unity destroys them. Look up “DontDestroyOnLoad” function.

If you don’t like the above solution, may be try reloading the scene inside the script within the same scene without actually calling the LoadScene method?

Good luck… Let me know if this helps…

Figured it out!
Instead of calling the StartDirectives object in the void awake, I put a call for it into the void start, this way it would call it on every start. Also- I separated the reload scene part to its own class. instead of just including it into the goalreached part in Directives, it now sends a broadcast message and is picked up by my new NextLevel class. this class set up the next level with a gradually increasing maze area, and an additional key to find, then reloads the scene with the new parameters.

I was hoping to get this working so I wouldn’t have to do separate scenes for each level, and this way the game will keep going on and on with no end…tehe.