- Home /
To pass a variable between scenes should I use scriptableobject or static ?
I have two scenes. In the first one the The Space Station scene I have a object name Player. The Player have a child a main camera.
I also have another scene name Main Menu when I'm running the game I want it to start with the Main Menu scene and the also the Main Menu scene main camera. If the Player will be on enabled true I will see the Player camera and the scene The Space Station will be the first one when running the game. So I turned off the Player.
Now when running the game it start with the main menu scene and the main menu camera.
In the main menu I have a button and I did that when I click on the button it will activate the Player and will unload the main menu scene:
This script is attached to a button on the main menu scene:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LoadSceneOnClick : MonoBehaviour
{
public static bool isPlayer = false;
public void ActivatePlayer()
{
isPlayer = true;
if (PlayerEnable.playerStat == true)
SceneManager.UnloadSceneAsync(0);
}
}
And this script is attached to an empty GameObject in the other scene The Space Station:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerEnable : MonoBehaviour
{
public GameObject player;
public static bool playerStat = false;
private void Update()
{
if (LoadSceneOnClick.isPlayer == true)
{
player.SetActive(true);
playerStat = true;
}
}
}
This way I can interact and access the Player object in both scenes. And it's working fine. My question is if I did it right ? If the scripts are fine and if it's logic to do it this way ? Or maybe I should do it somehow with scriptableobject class ?
If you're changing scenes, why wouldn't you just have the player active by default? When changing scenes, all old objects are removed (unless explicitly declared to remain there on scene change) and all new objects are loaded, so having the player inactive is unnecessary (since you won't see it until the scene is changed anyway).
The reason I can't change just the scenes is that on the second scene not the main menu I have some objects I want to be active when the main menu scene is active like sky box and a space station and some flocking objects all this give me environment and I want it to be show also when the main menu is active.
I will upload a screenshots. The first show the main menu you can see the main menu and the space station the sky box and more.
So now when running the game the active scene is The Space Station but Player is tuning off so I see the skybox and space station and flocking but the main camera is the camera of the main menu.
Now in the next screenshot when I click on Start Game button I just turn on the Player and removing main menu so now it's the Player main camera who is active. And now the The Space Station scene is in use:
It's hard to explain but when it's on the main menu scene it's using the main menu main camera and since player is turned off it will not use the player main camera. And since the active scene is the space station it will show the skybox thye spacestation flocking but the control will be of the main menu camera and scene.
After clicking on start game it's removing the main menu scene and turning on the player thats the second screenshot now the control is of the player and the player camera.
Answer by tormentoarmagedoom · May 13, 2018 at 09:38 AM
Good day.
When changing scenes, all Objects are deleted, and then all objects from new scene are instantiated., So, even if you have "identical" objects in both scenes, they are not the same object, they are different copies.
You must watch this Unity Tutorial about Persistence. It shows how to pass information between scenes, and how to save/load data when closing/starting the game again.
Bye!
Your answer
Follow this Question
Related Questions
How can i change the script format when creating a new one ? 3 Answers
How can i make that it will create cubes at mouse position even if i move the mouse fast ? 1 Answer
How can i make my soldier to patrol over the space station in random ? 1 Answer
Multiple gameobjects but only one is screen wrapping. 0 Answers
How can i set audio mixer groups volume to be used with ui slider when 0 is lower and 80 louder ? 1 Answer