hello,
So currently right now, I just have 1 prefab, which is the player that will go from 1 scene to another… and maybe, total wise, would have 5 variables static, from 1 scene to another, wondering what is best in the long run and performance for mobile, to use gameobject static, and reference like this:
void Start()
{
if (Main_Menu.playerInstance != null)
{
Debug.Log("Player found: " + Main_Menu.playerInstance.name);
}
else
{
Debug.LogWarning("Player not found in Scene 2!");
}
}
or should I create a separate object to reference the instantiated player object like this:
private GameObject player
void Start()
{
public GameObject player; // Assign player after scene loads
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
if (player != null)
{
Debug.Log("Player found: " + player.name);
}
else
{
Debug.LogWarning("Player not found in Scene 2!");
}
}
just wondering, what is best, but also again performance specifically, again its for mobile… i know mobile devices are fast now… but don’t want to have bad habits or have any or as much overhead… any thoughts? Again, the prefabs for now, is just 1 prefab… BUT, hoping to expand on this and learn multiplayer which then would or may turn into 4 prefabs.
Don’t think I’d do either of these. Why does the main menu need a static reference to the player?
Ideally the player should not be included in any scenes. Instead, it can be additively loaded in, and remain loaded while other scenes are loaded and unloaded around it.
A static singleton pattern can be used to let other systems access the player remotely that can’t otherwise get access via other means like collisions.
As an aside, there is exactly zero reason to use FindGameObjectWithTag
nowadays. Tags in general can be forgotten about as a feature. Ideally, all ‘Find’ methods should be avoided. If you must use one, use FindFirstObjectByType<T>
instead to get a strongly typed reference to a component, rather than the game object. It’s the components you care about 99% of the time.
thanks for the info, k, so the main menu was used for player customizations and such, and i have it instantiated there as well, so thats why, its a small script… not sure, if you can explain more or have a youtube example of the additively loaded the player prefab and remain loaded while other scenes are loaded around it? what I am doing is once the player prefab is instantiated, it doesnt get destroyed, instead it gets instantiated to the next scene… is that what you mean? also, so the first one for static GameObject you would recommend and not the “Find” methods?
I assume you mean DontDestroyOnLoad? That is an option, but that still requires having the player in one or more scenes. Which means you may accidentally reference is directly, which will cause issues when you leave and return a scene.
I don’t know any youtube tutorials on additive scene loading, but I’m sure they exist, so go searching and watch a few.
In any case its just a parameter when loading a scene: Unity - Scripting API: SceneManagement.SceneManager.LoadScene
You load scenes with LoadSceneMode.Additive
for the second parameter, which loads the scene but doesn’t unload existing scenes. When moving from one scene to another, you additively load the next scene, then unload the previous scene, which will keep the player scene loaded.
Watch some videos, play with the concept in isolation. Learn by doing.
I would recommend your player expressing a singleton pattern.
gotcha, ok, i see, this is great for probably unlimited run game or a LARGE game, found an asset and demo in the asset store:
but, good thing is, my game isnt that big… actually… Maybe i wasnt using the proper vocabulary… but I probably at most have 2 scenes, 1 is the menu scene, and 1 is the actual game scene… and its not very big… maybe a lot of objects in the game scene, but thats it… and I wanted my player prefab, to go from once scene to another, and it does work, i do have it going, but of the 2 choices… was wondering what is best…
here is my code:
// Main Menu Script
public static GameObject playerInstance { get; private set; }
public void StartSinglePlayerGame() // button calls on this Function
{
string LevelSelected = string.Empty;
LevelSelected = "Level_" + houseIndex;
SceneManager.LoadScene(LevelSelected);
DontDestroyOnLoad(playerInstance); // this is so player prefab doesnt get destroyed
}
// Scene Level_1 script
void Start()
{
if (Main_Menu.playerInstance != null)
{
Debug.Log("Player found: " + Main_Menu.playerInstance.name);
}
else
{
Debug.LogWarning("Player not found in Scene 2!");
}
}
Size of the game and number of scenes doesn’t matter here. It’s still a very common way to handle things even in smaller projects. You don’t need to buy an asset do to this either. It’s a few extra lines of code at most.
gotcha thanks, so i am probably going to add this then:
SceneManager.LoadScene(LevelSelected,LoadSceneMode.Single);
DontDestroyOnLoad(playerInstance);
if that makes sense, as 1 scene will be closing the current scene and load the Game scene.