Loading a new level, preserving an object

Hi I have a question about transferring values in between scenes.

I have a character mesh where I am letting the player customize the look of the mesh (hair color, etc). This all happens in a character creation scene.

Now when the player is done, they will click “done” and load into the 1st level of my game. I’m assuming I’ll be using “LoadLevel (name : string) : void”.

Obviously I don’t want the created character to be destroyed in between the loading of scenes, so I assume I have to use:
Object.DontDestroyOnLoad.

Does this sound correct so far?

Then once in the new scene, how do I spawn the non-destroyed object into the new level?

You don’t…since the object was never destroyed, it’s still there and doesn’t need to be spawned.

–Eric

Interesting, I guess I’ll just try it out, but does the object spawn in the exact same world space in level 1, in level 2?

It doesn’t spawn at all…it’s never deleted, so nothing changes.

–Eric

Okay thanks. I just tried it. Seems like it works but my player is located far away from where the second level geometry is. I want to relocated the mesh object from level 1 to be in at least the same vicinity!

So I know you can do this:

someObject.position.x = someIntPosition;

But I’m not sure what to enter for “someObject”. How do I get the name of the object I want to move? Is it just the name from the hierarchy? I named my player mesh “player”, but player.position.x = SomePosition doesn’t seem to be working…

You’ll need to grab a reference to the Player, so maybe using GameObject.Find(“Player”); on level load or awake(), and setting it to your location.

Something like…

GameObject player;
Vector3 levelStartPosition;

void OnLevelWasLoaded(){

     //assuming they're called player.
     //could also use a player tag
     player = GameObject.Find("Player");

     player.transform.position = levelStartPosition;

}

I doubt that would compile, but that’s the sort of thing I’d do.

I keep a ‘master class’ that persists through all levels, which loads references to say… a “Level Preferences” class which contains your level start positions etc, and then applies them to the main character (which it retains a reference to through DontDestroyOnLoad).

Good luck!

-BSpline