-1

I have a case of 2 scenes :

A - is a loadscreen where i proposed a number of buttons corresponding to different levels of the game

B - are a certain level of the game

A is generating the buttons by the code:

for (int i = 0; i < LevelNumber.nbLevels; i++) { GameObject newButton = Instantiate(levelButtonPrefab); newButton.GetComponent<LevelButton>().SetButton((i + 1).ToString(), "LevelG"); //connexion à LevelG newButton.transform.SetParent(grid, false); //The new created button is part of our grid } levelButtonPrefab is a prefab object. The activation and connexion to LevelG works well, but when i come back to scene A,game object newButton are destroyed.

How can i proceed to conserve newButton game object as i don't have newButton in Inspector ?

Thanks in advance,

ASSADOUR
  • 27
  • 4

2 Answers2

0

The Scenemanager in Unity has the option to load scenes additively. This would mean that both your A and your B scenes would exist at the same time. Keep in mind that objects in the one scene dont necessarily have access to one another unless you link them for exampe through code though. Or just have the A scene act as input receiver and GUI.

I would suggest you can have the loadscreen loaded in first, and then load the game scene secondarily as an additional scene.

SceneManager.LoadScene("YourGameScene", LoadSceneMode.Additive);

You should then be able to unload the game scene when you no longer need it. And to instead load any other scene needed after you unload it.

The advantage to the above described approach would be that you can create the buttons and UI in scene A using Unity's UI system. And then dont have to generate it using code.

This way your scene A becomes the HUD and scene B the game scene. And you can edit it using Unity's scene view instead of code.

Doh09
  • 2,324
  • 1
  • 16
  • 31
  • Thanks a lot, i would try to implement Additive in scene loading. But maybe i should deactivate the canvas grouping my buttons as the they still be visible through my game screen. – ASSADOUR Jan 21 '22 at 17:59
0

Try using the DontDestroyOnLoad() method. https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

elif
  • 1