References to Prefab get lost on start

I’m following a tutorial for a top-down RPG and in the beginning of this episode

he puts the player prefab(and the Canvas(stats) and Camera)in another scene so that,even when not starting from the main scene,there’s always one of these objects(so that testing is easier).

When he does that there isn’t any issue,but when I try it all the references to the Player and the other prefabs become null whenever the player moves from a scene to another.


This code is in the start function of the objects mantained between scenes

private static bool playerExists;//becomes true whenever a player object is created if there isn't another one in the current scene


if (!playerExists)
		{
			DontDestroyOnLoad (transform.gameObject);
			playerExists = true;
		} 
		else 
		{
			Destroy (gameObject);
		}

This code should destroy the prefabs in the current scene if ones from others are already present(this way there will always be one player,camera and stats per scene)


I think that this happens because in the start functions of objects local to the scene, the object references are set with FindObjectOfType() to the prefabs local to the scene(local player,camera and stats),but then they are destroyed and the new prefabs coming from another scene take their place,but with missing references.


VVV UPDATE:tested this hypotesis,put the result in reply VVV


What I don’t understand is this doesn’t happen in the tutorial,and before trying any wacky workaround I’d like to know why
if you need more specific info/code ask and I’ll post it ASAP

only read first few lines and it sounded right away like they are using a singleton method to have presistent data? is that correct? if so make sure you DontDestroyOnLoad is set to true. otherwise when you switch scenes it will destroy. if you have a singleton method and the object is in both scenes, whichever one was created first will survive and the others will automatically destroy on scene changes.

private static FlexibleMusicManager _instance;

public static FlexibleMusicManager instance {
	get {
		if (_instance == null) {//in case not awake yet
			_instance = FindObjectOfType<FlexibleMusicManager> ();
		}
		return _instance;
	}
}

void Awake ()
{
	// if the singleton hasn't been initialized yet
	if (dontDestroyOnLoad) {
		if (_instance != null && _instance != this) {
			Debug.LogError ("Duplicate singleton " + this.gameObject + " created; destroying it now");
			Destroy (this.gameObject);
		}

		if (_instance != this) {
			_instance = this;
			DontDestroyOnLoad (this.gameObject);
		}
	} else {
		_instance = this;
	}
}

I finally found a solution!
I discovered that the Destroy() method isn’t very reliable, and for some reason the references were set to the superflous prefab before its destruction.
I fixed this by using the DestroyImmediate() method and now everything works fine(but I read its use is not advised).

(I still put the singleton method inside of Awake() )


Still don’t understand why the guy from the tutorial didn’t have to use it.