Saving Position, Rotation and Scale of GameObject on Scene Transition

Hello everyone,

I have a game object in Scene 1, which the player moves, rotates, and scales. I want to save these three manipulations, so that the object remains as it was manipulated for the next 3 scenes.

I make the transitions with GUI buttons and SceneManager.LoadScene in my workflow. I have read about DontDestroyOnLoad but havent found an implementation that I understood and worked for me - a total newbie with Unity…

Thanks in advance!
Michael

Hey Michael,

What you need is a singleton. This will persist through scene changes. This is what a singleton may look like,

using UnityEngine;
using System.Collections;
using System;
using UnityEngine.SceneManagement;
//using System.Runtime.Serialization.Formatters.Binary;
//using System.Runtime.Serialization.Formatters.Binary;
using System.IO;




public class GameManager : MonoBehaviour 
{

	public static GameManager instance = null;

	void Awake () 
	{
		//Create one instance of object and make persistant.
		if(instance == null)
		{
			instance = this;
			//PlayerPrefs.DeleteAll();
		}
		else if(instance != this)
		{
			Destroy(gameObject);
		}
		DontDestroyOnLoad(gameObject);
       }

void Start(){}
void Update(){}
}

Once this script is saved, attach it onto an empty gameobject in your first scene. This will now be present in every scene, which means you can access it from any other script by simply typing,

GameManager.SavePosition(float x, float y, float z, float rot_x, float rot_y, float rot_z, bla bla bla);

Obviously that function must be coded into your GameManager. When you do get to that point, keep in mind all variables in the GameManager must be set to

public static bool example = true

The same can be said for your functions that you’ll wanna call,

public static void Example() { //do something }

That should get you off to a fighting chance, good luck, hope this was helpful!

Thanks for the answer!
I have read about the Singleton implementation but a guy told me to avoid it.
Since I am not a developer and what I am trying to do does not require “professional coding” it does not really matter.

I have the following script attached to an empty GameObject in Unity:

using UnityEngine;
using System.Collections;
using System;
using UnityEngine.SceneManagement;
using System.IO;

public class ObjectManager : MonoBehaviour 
{

	public static ObjectManager instance = null;

	void Awake () 
	{
		//Create one instance of object and make persistant
		if(instance == null)
		{
			instance = this;
		}
		else if(instance != this)
		{
			Destroy(gameObject);
		}
		DontDestroyOnLoad(gameObject);
	}
		
	void SavePosition()
	{
		PlayerPrefs.SetFloat ("posX", this.transform.position.x);
		PlayerPrefs.SetFloat ("posY", this.transform.position.y);
		PlayerPrefs.SetFloat ("posZ", this.transform.position.z);
		PlayerPrefs.SetFloat ("rotX", this.transform.eulerAngles.x);
		PlayerPrefs.SetFloat ("rotY", this.transform.eulerAngles.y);
		PlayerPrefs.SetFloat ("rotZ", this.transform.eulerAngles.z);
		PlayerPrefs.SetFloat ("scaleX", this.transform.localScale.x);
		PlayerPrefs.SetFloat ("scaleY", this.transform.localScale.y);
		PlayerPrefs.SetFloat ("scaleZ", this.transform.localScale.z);
	}

	void LoadPosition()
	{
		transform.position = new Vector3(PlayerPrefs.GetFloat("posX"), PlayerPrefs.GetFloat("posY"), PlayerPrefs.GetFloat("posZ"));
		transform.eulerAngles= new Vector3(PlayerPrefs.GetFloat("rotX"), PlayerPrefs.GetFloat("rotY"), PlayerPrefs.GetFloat("rotZ"));
		transform.localScale= new Vector3(PlayerPrefs.GetFloat("scaleX"), PlayerPrefs.GetFloat("scaleY"), PlayerPrefs.GetFloat("scaleZ"));
	}
}

Howerver, I am surely missing something and it does not work…How do i make reference to the game object, whose data I want to save across scenes?