save variable from previous scene


Been stuck with this problem for a while, and been playing with the scrpits a lot.

Trying to save the player chosen from scene number 1 and displaying it in scene number 2 .

In scene number 1 I have script named “SwitchPlayer”.
which contains a public static variable “chosenPlayer” that have the player chosen in this scene.

In scene number 2 have script named “WhichPlayer”.

which contains a public non static string variable “chosenPlayerName” .

chosenPlayerName =;

when switching from scene 1 to scene 2 this error shows:

MissingReferenceException: The object of type ‘GameObject’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Object.get_name () (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.bindings.cs:198)
whichPlayer.Update () (at Assets/whichPlayer.cs:36)

I am mentioning that I don’t want to use DontDestroyOnLoad on the first scene.

Please help!!

hello. you can not do it that way. the static var will persist through scenes, but the static var has a reference to the object player, so when you chaange of scene the player gets destroyed so when you access the var it says that it has been destroyed, if you only need the name of the object you can change the chosenPlayer to an string an save the inside since the name stores a copy of the player name not a reference to it.

Thanks a lot!!