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 ?