'Unity: Cannot Load/Save Player Pos

So, in Unity I am trying to load the player's position from PlayerPrefs. LOAD FUNCTION

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.IO;
using UnityEngine.UI;
using CI.QuickSave;

public class RELoadSave : MonoBehaviour
{

    public GameObject Player;

    public float x, y, z;
    private bool saving = false;

    private int Mode = 0;
    private Vector3 coordsem;
    private int count;

    protected FileInfo theSourceFile = null;
    protected StreamReader reader = null;
    protected string text = " "; // assigned to allow first line to be read below

    public Button btn;
    void Start()
    {
        x = PlayerPrefs.GetFloat("x");
        y = PlayerPrefs.GetFloat("y");
        z = PlayerPrefs.GetFloat("z");

        Vector3 LoadPosition = new Vector3(x, y, z);
        Player.transform.position = LoadPosition;
    }
    private void Awake()
    {
        
    }

    void Update()
    {
        
    }
    public void ResetScene()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        

    }

}

SAVE FUNCTION

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
using TMPro;
using UnityEngine.UI;
using CI.QuickSave;

public class SaveGame : MonoBehaviour
{
public float coordse;

public float x, y, z;

public GameObject Player;

// Start is called bsefore the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    
}

public void SaveGameCore()
{
    x = Player.transform.position.x;
    y = Player.transform.position.y;
    z = Player.transform.position.z;

    PlayerPrefs.SetFloat("x", x);
    PlayerPrefs.SetFloat("y", y);
    PlayerPrefs.SetFloat("z", z);

}

}

Things to note:

  • these functions are NOT called inside the player
  • running Unity 2019 This is meant to be applicable to a 3d FPS game, taht is what the code is for not a 2d game. BUT I am confused, but I probally made a simple mistake. If it is not replacatable, I can upload small assets from the game. It should be working but it isn't. The player also can only collide with 2d box coliders if that helps.


Solution 1:[1]

I checked your code and everything works fine

public float x,y,z;
public GameObject Player;
public Vector3 LoadPosition;
public void Save()
{
    x = Player.transform.position.x;
    y = Player.transform.position.y;
    z = Player.transform.position.z;

    PlayerPrefs.SetFloat("x_", x);
    PlayerPrefs.SetFloat("y_", y);
    PlayerPrefs.SetFloat("z_", z);

}
public void Load()
{
    x = PlayerPrefs.GetFloat("x_");
    y = PlayerPrefs.GetFloat("y_");
    z = PlayerPrefs.GetFloat("z_");

    LoadPosition = new Vector3(x, y, z);
    Player.transform.position = LoadPosition;
}

The problem may be related to the method of calling save or laod

Perhaps the problem is with the version of the unity you have, this is an unlikely possibility

Anyway, you can do without PlayerPrefs by saving the player data (Positino) in a text file inside the folder

Take a look at this reference for saving and Load

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1