Skip to main content
edited title
Link

Object atomaticautomatic create new with old every time when load / save game.?

Source Link

Object atomatic create new with old every time when load / save game.?

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.

First scene without save enter image description here

Second scene with save game.

enter image description here

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;

 }



}