- Home /
how to load a different save position in different scene?
I have 2 scene, one is the main menu and one is the main game. in the game i place may save point and in the main menu i have a 2 button, 1 is new game and 1 is continue. what i already did is i can play the new game simple by loading the second scene, but for continue i cant load the previous save point by using playerprefs. only the continue button in my game is the one not functioning right
this is my code for the save point
public class save : MonoBehaviour
{
public bool triggered;
public Animator anito;
void OnTriggerEnter2D(Collider2D cut)
{
if (cut.gameObject.CompareTag("Player"))
{
anito.SetBool("save", true);
PlayerPrefs.SetFloat("PlayerX", transform.position.x);
PlayerPrefs.SetFloat("PlayerY", transform.position.y);
PlayerPrefs.SetFloat("PlayerZ", transform.position.z);
PlayerPrefs.SetInt("Saved", 1);
PlayerPrefs.Save();
triggered = true;
}
}
void OnTriggerExit2D(Collider2D cut)
{
if (cut.gameObject.CompareTag("Player"))
{
anito.SetBool("save", false);
triggered = false;
}
}
}
and this is for the continue button this code work if the continue button is in the same scene. obj2 is the player object
public void continuegame()
{
obj2.transform.position = new Vector3(PlayerPrefs.GetFloat("PlayerX"), PlayerPrefs.GetFloat("PlayerY"), PlayerPrefs.GetFloat("PlayerZ"));
obj3.SetActive(false);
obj4.SetActive(true);
}
Answer by Llama_w_2Ls · Jan 11, 2021 at 02:12 PM
I think I get what your problem is. When loading a new scene, the object in question is given the position in the editor, and not the saved position. Can't you use a script on the start method of the object, that runs this code transform.position = new Vector3(PlayerPrefs.GetFloat("PlayerX"), PlayerPrefs.GetFloat("PlayerY"), PlayerPrefs.GetFloat("PlayerZ"));
and if the floats were not found (e.g. New Game), don't run that line? @Sad_Man30
is the only way to call the other scene is this Scene$$anonymous$$anager.LoadScene("SampleScene");
??
Pretty much. You have to use the Scene$$anonymous$$anager.
what i want to happen is to call the other scene but load it save point. the continue button is in the main scene and the save point is in the sample scene. for me to get to the other scene i use that Scene$$anonymous$$anager.LoadScene("SampleScene");
but it always reset the scene so it will always load at the start.
Could you not use PlayerPrefs, to access the position of the save point from any scene?
Your answer
Follow this Question
Related Questions
[ANSWERED]UNDERTALE Game Over screen [2D] 1 Answer
8-directional orientation, top down 2D, seperate from movement 0 Answers
2d top-down rpg direction check 1 Answer
How would you make a 2D NPC walk from one point to another for a cutscene? 1 Answer
How to share a variable through multiple copys of the same script 2 Answers