My problem with this DontDestroyOnLoad(cube.gameObject); When I load my player position its create new object with default and saved position respectively. I have a screen shot for better understand.
Second scene with save game.
Here is my code.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
public class Player : MonoBehaviour {
public static float speed;
//public static Player control;
public float moveSpeed = 10f;
public float turnSpeed = 50f;
public Transform cube;
public float x, y, z,x1 ,y1, z1;
public int s;
//public static List<Game> savedGames = new List<Game>();
public static Player Instance;
// Use this for initialization
// Update is called once per frame
void OnTriggerEnter(Collider other) {
if(other.tag == "Player1"){
DontDestroyOnLoad(cube.gameObject);
//DontDestroyOnLoad (other.gameObject);
}
}
void Awake ()
{
if (Instance == null) {
DontDestroyOnLoad (this.gameObject);
} else {
DestroyObject(gameObject);
}
}
public void Start () {
speed = 50f;
}
//Update is called once per frame
public void Update () {
if(Input.GetKey(KeyCode.W))
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.S))
transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.A))
transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.D))
transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
}
public void Save(){
//Player.savedGames.Add ();
BinaryFormatter bf = new BinaryFormatter ();
FileStream fs = File.Create ("D:/playerinfo3.txt");
playerData pd = new playerData ();
PlayerPrefs.SetInt ("currentscenesave",SceneManager.GetActiveScene().buildIndex);
PlayerPrefs.SetFloat ("cubeposx",cube.position.x);
PlayerPrefs.SetFloat ("cubeposy",cube.position.y);
PlayerPrefs.SetFloat ("cubeposz",cube.position.z);
PlayerPrefs.SetFloat ("cuberotx",cube.eulerAngles.x);
PlayerPrefs.SetFloat ("cuberoty",cube.eulerAngles.y);
PlayerPrefs.SetFloat ("cuberotz",cube.eulerAngles.z);
pd.x = PlayerPrefs.GetFloat ("cubeposx");
pd.y = PlayerPrefs.GetFloat ("cubeposy");
pd.z = PlayerPrefs.GetFloat ("cubeposz");
pd.x1 = PlayerPrefs.GetFloat ("cuberotx");
pd.y1 = PlayerPrefs.GetFloat ("cuberoty");
pd.z1 = PlayerPrefs.GetFloat ("cuberotz");
pd.s = PlayerPrefs.GetInt ("currentscenesave");
SceneManager.LoadScene (pd.s);
bf.Serialize (fs,pd);
fs.Close ();
}
public void Load(){
//transform.position = new Vector3 (PlayerPrefs.GetFloat ("cubeposx"), PlayerPrefs.GetFloat ("cubeposy"), PlayerPrefs.GetFloat ("cubeposz"));
if(File.Exists("D:/playerinfo3.txt"))
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream fs = File.Open ("D:/playerinfo3.txt",FileMode.Open);
playerData pd = (playerData)bf.Deserialize (fs);
pd.x = PlayerPrefs.GetFloat ("cubeposx");
pd.y = PlayerPrefs.GetFloat ("cubeposy");
pd.z = PlayerPrefs.GetFloat ("cubeposz");
pd.x1 = PlayerPrefs.GetFloat ("cuberotx");
pd.y1 = PlayerPrefs.GetFloat ("cuberoty");
pd.z1 = PlayerPrefs.GetFloat ("cuberotz");
fs.Close ();
SceneManager.LoadScene (PlayerPrefs.GetInt ("currentscenesave"));
cube.position = new Vector3 (pd.x,pd.y,pd.z);
cube.rotation = Quaternion.Euler (pd.x1,pd.y1,pd.z1);
Debug.Log ("currentscenesave"+ " "+ pd.s);
Debug.Log ("currentscenesave"+ " "+ pd.x);
Debug.Log ("currentscenesave"+ " "+ pd.y);
Debug.Log ("currentscenesave"+ " "+ pd.z);
}
}
[Serializable]
class playerData{
public float x, y, z;
public float x1, y1, z1;
public int s;
}
}


DontDestroyOnLoadon anything you want to reset when loading a savegame. Do you have a good reason why you have it? Note that since Unity5 you can load and unload scenes additively (add a scene without leaving the previous one) which solves many situations where you would otherwise have to useDontDestroyOnLoad. \$\endgroup\$DontDestroyOnLoadI am not able to load player position. @Philipp \$\endgroup\$DontDistroyOnLoadI can not get my player to next scene after trigger new scene @Philipp \$\endgroup\$