-3
\$\begingroup\$

I have problem with scene's and objects' positions. My problem is that the position of object/player changes after I load the scene. I don't know why this happens.

I have saved my object position with "playerprefs". The object keeps its position if I remove the code that loads the scene but if I write the one line of code to load the saved scene with object/player position, the scene completely reloads with (0,0,0) as its position.

I have seen that Unity player first loads the cube position and then loads the scene, but why? Because of this my cube position changes to default position (0,0,0). How can I load first the scene and then cube position?

If any one can help me to keep position of object/player with loaded saved scene then many many thanks.

Here is my code:

public void save(){

    BinaryFormatter bf = new BinaryFormatter ();
    FileStream fs = File.Create ("D:/playerinfo.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);

    pd.x = PlayerPrefs.GetFloat ("cubeposx");
    pd.y = PlayerPrefs.GetFloat ("cubeposy");
    pd.z = PlayerPrefs.GetFloat ("cubeposz");
    pd.s = PlayerPrefs.GetInt ("currentscenesave");
    //cube.position = new Vector3 (PlayerPrefs.GetFloat ("cubeposx"), PlayerPrefs.GetFloat ("cubeposy"), PlayerPrefs.GetFloat ("cubeposz"));

    bf.Serialize (fs,pd);
    fs.Close ();        
}

public void Load(){ 
    //loadScene ();

    BinaryFormatter bf = new BinaryFormatter ();
    FileStream fs = File.Open ("D:/playerinfo.txt",FileMode.Open);
    playerData pd = (playerData)bf.Deserialize (fs);

        fs.Close ();
        SceneManager.LoadScene (pd.s);  
        cube.position = new Vector3 (pd.x,pd.y,pd.z);

    }
}   
\$\endgroup\$
0
\$\begingroup\$

Note: I'm by no means the lead authority on this, but I believe you've combined two different methods of saving the data.

Player Prefs don't require the binary formatter stuff Binary Serialization does not use player prefs

I'd say player prefs are easier to use (although not as great for saving lots of data)

To save the PlayerPrefs it's just PlayerPrefs.Save() -Here's a useful link to the docs: http://docs.unity3d.com/ScriptReference/PlayerPrefs.html

Actually meant to be revising at the moment so my explanation isn't perfect, ask if you need more.

Can't test it but try something on the lines of this:

public void Save(){    
    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.Save()      
}

public void Load(){ 
    //loadScene ();

   cube.position = new Vector3 (PlayerPrefs.GetFloat ("cubeposx"), PlayerPrefs.GetFloat ("cubeposy"), PlayerPrefs.GetFloat ("cubeposz"));

    }
}  
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I think you didt read my question properly. I said scene reload after get cube position. thats why cube position is changed to (0,0,0,). It doest matter I used binary formatter or not. And one more thing if I remove Scenemanager.LoadScene(); then its work perfect with both binary formatter and playerprefs. so forget binary formatter and concentrate on main point. its reload all object position to default after load scene. @Ben B \$\endgroup\$ – hardwork Apr 22 '16 at 3:24
0
\$\begingroup\$

When you load a scene from a script you cant execute code after that.You need to deserialize your player data and then set the position in a Awake() or Start() method. They get called once when the scene is loaded.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I change my mind for this. First can you tell me can set player position and scene together. If i have 3 scenes and i reached to 2 and a half scene and i want to save then how can i do it. This code relode my player position so I let it. \$\endgroup\$ – hardwork Apr 26 '16 at 12:49
  • \$\begingroup\$ what is better save different scenes with player position or change and save object and textures with player position.? \$\endgroup\$ – hardwork Apr 26 '16 at 12:50
  • \$\begingroup\$ I dont think you should save the scene at all. just the positions and states of objects in the scene that you want to save. Then when the scene loads simply fill in the date in a Awake(). Watch this : unity3d.com/learn/tutorials/modules/beginner/… \$\endgroup\$ – Uri Popov Apr 26 '16 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.