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