-2

I am making a maze game, but I'm not sure how to do what I have in mind:

I want to copy the random generated maze to another scene, with a button, I have generate maze button and I want to make play button, which saves the maze and transitions the scene to the playable character into the maze. Is there a way to save the positions of all the game objects, or just copy pasting it automatically?

Thanks in advance.

Wolf
  • 9,679
  • 7
  • 62
  • 108
  • Are you generating at runtime or design-time? If the former why not just generate when the destination scene is loaded? Otherwise if the latter perhaps _pre-fabs_ is the way to go? –  Jan 17 '21 at 00:57
  • I'm generating them at runtime ,basically I put in the parameters width and height ,then clich "generate" and every time i click the button a new maze is generated ,since its for uni they'll probably generate a few maps before they actually start the game. – Радо Манолов Jan 17 '21 at 01:28
  • I still think you are doing it backwards (the maze should be generated in the current scene). Surely you could just have one scene and generate the random maze there. No copy required. –  Jan 17 '21 at 02:14
  • Well,since I'm not that knowledgable about unity,how can i switch from like the main menu in which i generate the maze,to a character in the maze? – Радо Манолов Jan 17 '21 at 12:10
  • 1
    Probably a duplicate of [How to pass data between Scenes](https://stackoverflow.com/questions/32306704/how-to-pass-data-between-scenes-in-unity) – derHugo Jan 17 '21 at 15:39
  • Please update the question to include the details you have given in comments. – Wolf Jan 17 '21 at 16:12

2 Answers2

1

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.

Bola Gadalla
  • 370
  • 3
  • 14
  • Well ,I am dealing with alot of prefabs ,lets say I'm making a maze 4x4 ,its 16 squares ,but before the hunt and kill algorithm carves the path,there are 80 children of the maze object,when the path is carved and finished ,there are like lets say 35 left ,and all that depends on the size of the maze,on a 2x2 you start with 20 gameobjects of walls and floors and then it goes down to like 10. On my main scene,i have like a menu which ahs width and height ,and when you input it and generate it ,there is a preview of the maze,so im not sure how to deal with that . – Радо Манолов Jan 17 '21 at 12:16
0

Yes you can save the positions of GameObjects. I have only used Unity for game development and the way it would work here is each 'Wall' GameObject would be a child of a 'Maze' GameObject. The locations of the walls are relative to the parent.

James Webb
  • 167
  • 2
  • 17
  • 1
    Okay ,do you have any examples about that ? I've made the floor and walls a prefab,but i don't exactly know how to access them frmo their parent ,I'm still new to this kind of thing not gonna lie. I've made a 2 layer array to store the columns and rows of the floor and walls,I'm using the hunt and kill algorithm to make the maze,and it basically carves it's way trough a solid grid ,so I'm not sure exactly how to save the location of the finished maze. – Радо Манолов Jan 17 '21 at 02:02
  • @MickyD The question was extremely abstract and non specific and so is my answer. – James Webb Jan 18 '21 at 08:48
  • @РадоМанолов please provide examples of what you have achieved so far. This will help to understand what you are trying to achieve and what's going wrong. – James Webb Jan 18 '21 at 09:03
  • @MickyD I didn't see a problem with the question so I didn't flag. The question seemed to ask if something was possible in a general way so I gave a general answer. I'm not really sure what you are suggesting I elaborate on as if I made my answer any more specific it may not have helped OP with their question. – James Webb Jan 18 '21 at 10:15