Accessing DontDestroyOnLoad hierarchy from script

I have a player model in a character select screen that I’m trying to import to a game scene proper and I’ve taken to using DontDestroyOnLoad() to do it. For some reason, though, I can’t access that model from within a script in the scene it’s being loaded into. I tried a simple print() method just to see if the script would recognize it was there and I get nothing. How, then, am I suppose to code a way to see if the model exists in the DontDestroyOnLoad hierarchy or not if the script can’t seem to even see what’s in that hierarchy in the first place?

I’m not aware of a way to access the “dontdestroyonload” section. Generally when you use something like this, it’s with singletons or you use some way to reference it directly. If you want to use it for a model like what you say, then you need to create a reference to it after the scene loads or you have a reference on the script.

You could find it by a tag, or a script, or however.

Create a reference to it after it loads as in when the new scene loads? How do I do that?

Like I said, either you go with Singleton pattern where you can access the stuff through the static instance of a script or when your new scene loads, you should be able to do a FindWithTag type of call if your player has it’s own tag.

I tend not to move models around, but prefer to move data myself, which I can then apply to the new scene, so I don’t have an example, but I think there are some floating on the forums.

Well, in truth all I need to be able to do is move one int variable from one scene from another so how do I do that?

Well, either make a static int variable…or, for something that simple, a playerpref would be fine. Just save the playerpref in the first scene, then access it in the second scene.

Playerpref is good if you want to keep track of it even if the player closes the game and starts again. (maybe to remember the last choices they made)

I tried making a static int and it didn’t work. As for Playerpref, I just need it from the end of the first scene to the beginning of the second scene and then I want it gone. Plus I’ve heard a lot of unsavory things about Playerprefs in general.

The quick way I do this is just create a script and throw it on an object. In awake or start you set dontdestroyonload. Create your public parameters or public methods you need for your other objects/scripts to get the information you want them to access from this object. No need to set them as static. Just set either the parameter public, or set your method public you want to use to get that information.

Then assign a tag to this object. All other scripts that need this information do something like this:

int valueFromPreviousScene = GameObject.FindObjectWithTag("SceneTraveler").GetComponent<CoolScriptThatSurvivedSceneChange>().ParameterThatMadeItIntoNewScene;

Not more complicated than that. In this example the object that came from the previous scene has the tag “SceneTraveler”, with a script attached called “CoolScriptThatSurvivedSceneChange” with a public int parameter called “ParameterThatMadeItIntoNewScene”. You can do this with a lot more than just ints. As far as I know you can do it with anything you can set as a parameter. References to prefabs, custom data types, etc.

You can make this more robust later by first checking if the object even exists before trying to pull the value from it, then optionally instantiate the object with dummy values. Useful for testing a scene on its own in the editor without first having come from the expected previous scene.

Why didn’t your static int work? Did you declare it correctly.
There is nothing wrong with playerprefs for something like this.

Otherwise @Joe-Censored wrote up the other way that I mentioned.