So, I’m developing a game in which there’s time travel, and the player is allowed to move between two scenes (present and past). The problem is, although I can bring objects from one scene to other using “DontDestroyOnLoad”, their references in Scripts aren’t brought.
Example: if one of the scripts there’s a reference to the player “public gameObject Player;” this reference becomes “missing” in the next scene, as the object “Player” is destroyed in the change of scenes.
How can I attach the new Player of this new loaded scene to the script without adding a “DontDestroyOnLoad” to the player as well?
PS: I’ve tried to call Player = GameObject.Find(“Player”); inside Update, with no sucess
If you have a reference to an object in one script, and that object gets destroyed at the change of the scene you will lose it, even if there is that same player object in the other scene.
if in the new scene exist the GameObject called Player you could access it like you tested, but dont do it in the Update since that being called all the time.
You should have a OnLoad method in which you could set up all your vars from that need to reference the new object.
You can subscribe to SceneManager.sceneLoaded if you need any method to be called when loaded.
your approach is wrong from the start and will only cause you issues and headaches, all along the way, possibly to a point where you’re going to code yourself into a blind alley from which there will be no way out except scrapping most of your project and starting over, with a different approach. very likely the one I am going to suggest now:
you shouldn’t have the past and the present as two separate scenes. just have them grouped under two root objects on a single scene, and toggle those objects on and off as the player is moving back and forth in time.
meaning in your scene, you’ll have an object “Present”, which will be just an empty transform, and everything that exists only in present will be its child. Then you’ll have an object “Past”, with everything that only exists in the past being child of that. And a third one, something like “Both”, where everything that’s the same for both present and past will be (so, player, camera, etc). Then, start the game with Past being disabled, and Present active. When player travels, disable Present, and enable Past.
Couldn't agree more, I was trying to do it the most difficult way. Your approach is much simplier, I feel ashamed to not have thought of this myself hahaha Thanks ^^
Player = GameObject.Find(“Player”) this code finds the object which is just in current scene.
Use the script below for endless life
public class Player : MonoBehaviour {
public static Player instance;
private void Awake()
{
instance = this;
DontDestroyOnLoad(this);
}
//your other code
}
You can call it from another scene by using the code below
Couldn't agree more, I was trying to do it the most difficult way. Your approach is much simplier, I feel ashamed to not have thought of this myself hahaha Thanks ^^
– Vinytt