Resetting objects to level start position?

Hi,

Short story:
I was just wondering if there was any simple way in unity to reset physical and kinematic objects to the state (position + physical forces) in which they are when the level is started.

Long story:
Thing is that I want to reset my level without having to reload it. The latter is actually working fine but leads to a delay on the iPhone when the user wants to restart the level. The lag is just about a second or 2 but i feel like it would help a lot if the restart was faster. What I’m looking for is something simple like Application.resetObjects(). I’ve already gone through the hassle of trying to add scripts to record the position of each and every entity. The code for this just wasn’t very pleasing and also physical objects would not be reset completely. E.g. they would be set to their original position but keep tumbling as they still had forces applied to them which still acted after the reset.

When I need to “reset” some objects in a level, especially if they have existing motion/physics in process, I will normally call Destroy() on the objects in question and then re-instantiate them from a prefab back at the level start.

I realize you’re trying to avoid re-loading, but you only re-instantiate the objects you need and as I understand it, instantiating a prefab is faster after it has been done the first time. Anyone is welcome to correct me if I’m wrong on that point.

Thanks for the input. Will give that a try. Are you using the Object.Instantiate method or the EditorUtility.InstantiatePrefab one?

Nevermind. Got the cloning to work. There’s a problem though. Some of my scripts are referencing the GameObjects i try to destroy and re-instantiate. Once the instantiation is done, those scripts will report that im trying to access a destroyed object.
Not really sure on how to work around this. Any idea?
For example this happens when destroying the main player character. Cameras that have him as target transform will be reporting the error when destroying.

Yeah - Have you used tags? I would do a null check on the player object reference in the camera (or wherever) and then if it comes up null, do GameObject.FindWithTag(“Player”). I would then reassign the reference to the object found with that tag. There is a pre-existing tag for the player, but you can create your own. Make sure the prefabs that you’re instantiating also have the appropriate tags (or tag them after you create them using the gameobject.tag property).

If you’re unfamiliar with tags, check here.

Why not just save any state\score information you need from the level, and just reload it?

Simple: Because this causes quite a lag on the iPhone. The lag is just about 1-2 seconds but for this type of game it would add a lot to the value if the reloading would happen instantly.

I had my own ‘reset’ function that I called that would reset everything back to its initial position without reloading the level.

Its extra work to correctly store and apply that data when your reset function is called but its not difficult to do.