How to preserve the position of objects moved during gameplay.

Hi all!

I need to preserve the position of some objects once I have moved them during gameplay (game being played in the ‘Game’ tab in the Unity editor)

What are the possible alternatives to do that?

Has anyone ever written a script to facilitate this?

Many thanks!

Dan,

Create a vector3 var to assign the position to, then read it when you need it.

var tempPos = Vector3(0,0,0);
//---------------------------------------------------------------------//
function Start() {
  tempPos = transform.position;
  print("tempPos = " + tempPos + " @ " + name);
}
//---------------------------------------------------------------------//

Hi Black Mantis, thanks for taking the time!

What I meant is that once the game is ended that I can extract the position of some objects that I have set in game… In your code example, would ‘tempPos’ still have the in-game location once Unity goes from gameplay mode to editing mode?

Thanks again!!

Dan,

Once you reference the vector3 you can save it in playerPrefs. Then load the positions from prefs, re assign them to the objects.

Hi Black Mantis,

OK, that is clearer.

However, as this is probably a common need, are you aware of a script or add-on that can preserve a bunch of variables / objects between the boundaries of game-mode / edit-mode?

Thanks! :slight_smile:

I believe there is a system or two for that at the Unity Asset Store.

The systems in the Asset store are designed to place one script on each Obj.

I think I may have a script I wrote a while back that is a solution for an array of objects, and it saves the position and rotation of 25 or 50 Objects. I cant remember lol. If you want something like the solution I wrote just say so and ill try to find it.

Hi Black Mantis, thanks again…

I’ve been scouring the asset store and so far the “Unity Serializer” looks like it could be useful for my game, but I have yet to find out if it will carry out the above requirement.

I’ll ask the author at http://whydoidoit.com/ and report back!

Thanks!

Dan,

http://wiki.unity3d.com/index.php/ArrayPrefs2

–Eric