- Home /
How to attach game objects to scripts via scripting?
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
Answer by sh_code · Jan 17, 2019 at 02:55 PM
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 ^^
we all sometimes get fixated on a specific approach and it makes us overlook any other.
the key is: if you get stuck on an issue like this, where you even find something that looks like a partial solution (DontDestroyOnLoad), but after using it, there's some other weird unexpected problem that seems inappropriately hard to solve (which usually signals the partial solution wasn't supposed to be used for what you're trying to use it), just stop for a while, and (in your $$anonymous$$d, not actually!) throw away everything you've designed and written, and try again from zero, or at least from before the design decision which sent you on that complicated path (in this case it was the separation into two scenes), and consciously think of anything and everything that would help you avoid that.
(not trying to be an unnecessary smartass now, just giving a more general thinking technique that helps, at least to me personally, spot and avoid these blind alley paths ;) ) oh, and glad i helped :)
Answer by xxmariofer · Jan 17, 2019 at 01:40 PM
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.
Answer by badadam · Jan 17, 2019 at 03:03 PM
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
Player.instance;
Your answer
Follow this Question
Related Questions
GameObject references runtime script in scene file. Unsure whats wrong 2 Answers
How would I search my scene for a tag 1 Answer
need some help with this things 0 Answers
Saving Player Position between scenes? 1 Answer
To pass a variable between scenes should I use scriptableobject or static ? 1 Answer