0
\$\begingroup\$

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;

 }



}
\$\endgroup\$
5
  • \$\begingroup\$ The obvious solution would be to simply not use DontDestroyOnLoad on 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 use DontDestroyOnLoad. \$\endgroup\$
    – Philipp
    Apr 29, 2016 at 14:28
  • \$\begingroup\$ without DontDestroyOnLoad I am not able to load player position. @Philipp \$\endgroup\$
    – mahesh
    Apr 29, 2016 at 14:33
  • \$\begingroup\$ And also without DontDistroyOnLoad I can not get my player to next scene after trigger new scene @Philipp \$\endgroup\$
    – mahesh
    Apr 29, 2016 at 14:35
  • \$\begingroup\$ You said that i can load and unload scene additively. can you post example code.pls or link of tutorial @Philipp \$\endgroup\$
    – mahesh
    Apr 29, 2016 at 14:45
  • \$\begingroup\$ I wrote an answer. \$\endgroup\$
    – Philipp
    Apr 29, 2016 at 14:49

1 Answer 1

0
\$\begingroup\$

You can solve this by using the ability of the SceneManager to load scenes additively.

Create one base-scene with everything which is supposed to be present in every level (like the player-character, for example). Nothing in this scene requires DontDestroyOnLoad.

Create every level as a separate scene. Those scenes should not contain anything from the base-scene.

When you switch to another level (savegame, level select, game progression or otherwise), first unload the previous level (if loaded) with UnloadScene(currentLevel). Then load the new level with SceneManager.LoadScene(newLevel, LoadSceneMode.Additive).

LoadSceneMode.Additive loads the scene without destroying anything in the previous scene. When you then unload a scene which was previously loaded this way, anything which originates from a different scene stays intact. This allows you to switch out your levels without losing anything from the base-scene.

\$\endgroup\$
2
  • \$\begingroup\$ This work fine. But i am unable to unload scene. void OnTriggerEnter(Collider other) { OnLevelWasLoaded (level:2); } void OnLevelWasLoaded(int level) { if (level == 2) //print("Woohoo") SceneManager.UnloadScene(2); SceneManager.LoadScene (4,LoadSceneMode.Additive); } This is my code. \$\endgroup\$
    – mahesh
    Apr 29, 2016 at 16:55
  • \$\begingroup\$ I manage it. destroy object of old scene and problem solve. thanx \$\endgroup\$
    – mahesh
    Apr 29, 2016 at 17:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .