You can create a Singleton script and attach it into a gameobject, anything that is a child of that game object will not be destroyed when you move to the next scene. What i mean by destroyed is that when you move from one scene to another, all the object in the previous scene gets destroyed. But in the case of a singleton, the gameobject with the singleton persist to the next scene and so is any of its children. Here is the code i use for that:
public class GameSession : MonoBehaviour
{
/// <summary>
/// This is Singleton
/// </summary>
void Awake()
{
//Finds how many game sessions are there in the scene
int gameSessionCount = FindObjectsOfType<GameSession>().Length;
// If there is more then one gamesession then destroy this gameobject
if (gameSessionCount>1)
{
Destroy(gameObject);
}
// Else it would not be destroyed
else
{
// Doesnt Destroy this gameobject when the scene loads
DontDestroyOnLoad(gameObject);
}
}
}
What the code basically does in the Awake method (which is the first method that gets trigged before loading the scene) is looking to see if there is other GameSession(s) in the game hierarchy, if there already one then destroy this gameobject. And the other GameSession that wasn't destroyed would be persistent to the next scene as well. And so, if you attach the maze gameobject to the gameobject with the GameSession singleton as a child, then when you go to the next scene, it would be persistent with the parent and doesn't get destroyed. Plus, it keeps all its transforms and components.
Now am not sure how big your scene is or how many gameobjects you have in the hierarchy, but if you have a lot then i would find a different singleton that uses a static class instead of this way. That is because FindObjectsOfType<GameSession>()
loops through ALL gameobjects in your hierarchy and returns a list of gameobjects with that type, but still loops through all gameobjects to find the once with the GameSession class.
So if you have a huge amount of gameobjects i would recommend you find a better way to implement singleton, but they all will work the same.