Loading player's position from other scene.

I’ve set up a script for saving and loading the player’s data and its position. The game has an autosave systems which is activated once the player beats a level. I want that level to be loaded, the data is loaded sucessfuly, but the player appears on a totally black screen.

Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class leveluprare : MonoBehaviour {

    public static int RareObjectCount;
    public static int xp = 0;
    // Use this for initialization
    void Start () {
        RareObjectCount = PlayerPrefs.GetInt("Rare");
        xp = PlayerPrefs.GetInt("XP");
    }
   
    // Update is called once per frame
    void Update () {
        if (sistemadepuntos.SceneChange == true)
        {

            PlayerPrefs.SetFloat("PlayerX", transform.position.x);
            PlayerPrefs.SetFloat("PlayerY", transform.position.y);
            PlayerPrefs.SetFloat("PlayerZ", transform.position.z);
            PlayerPrefs.SetInt("Rare", RareObjectCount);
            PlayerPrefs.SetInt("XP", xp);
        }



        if (Input.GetKeyDown(KeyCode.R))
        {
            RareObjectCount = 0;
            xp = 0;
        }

        if (Flashlight.fDeath == true)
        {
            RareObjectCount = 0;
            xp = 0;
        }
    }

    public void OnTriggerEnter(Collider other)
    {
        if (other.name == "Rare")
        {
            RareObjectCount += 1;
            xp += 100;
            Destroy(other.gameObject);
        }

        if (other.name == "Punto")
        {
            xp += 50;
            Destroy(other.gameObject);
        }

        if (other.tag == "Enemy")
        {
            xp = 0;
            RareObjectCount = 0;
        }

        if (other.name == "Compound")
        {
            xp = 0;
            RareObjectCount = 0;
        }
    }

   



    private void OnGUI()
    {
        GUI.Label(new Rect(10, 20, 100, 20), "XP = " + xp);
        GUI.Label(new Rect(10, 65, 100, 20), "Rare = " + RareObjectCount);
    }

   public void Save()
    {
        PlayerPrefs.SetFloat("PlayerX", transform.position.x);
        PlayerPrefs.SetFloat("PlayerY", transform.position.y);
        PlayerPrefs.SetFloat("PlayerZ", transform.position.z);
        PlayerPrefs.SetInt("Rare", RareObjectCount);
        PlayerPrefs.SetInt("XP", xp);
    }

    public void Load()
    {
       float x = PlayerPrefs.GetFloat("PlayerX", transform.position.x);
       float y = PlayerPrefs.GetFloat("PlayerY", transform.position.y);
       float z = PlayerPrefs.GetFloat("PlayerZ", transform.position.z);
       RareObjectCount = PlayerPrefs.GetInt("Rare");
       xp = PlayerPrefs.GetInt("XP");

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

    }
}

I see you have a Save and Load function but you are just directly calling SetFloat and GetFloat elsewhere - I suspect that this code duplication is probably related to your problem. Unless Load/Save are being called from elsewhere I don’t see anywhere that you could possibly be “loading” your saved position. Should you maybe be calling Load() from Start()? And Save() from your if statement in Update?

Other things you’d benefit from changing:

  1. You’re using PlayerPrefs for something it was never meant to be used for, and that’s making everything here more difficult than it needs to be. PlayerPrefs, as the name suggests, is really only good for preferences, and for anything more complex than “Music volume” it’s going to lead to hugely inflated and complex code. For saving game data, learn to use some kind of serializer instead - look up a JsonUtility tutorial for a good place to get started with that. With serialization, you can call a single Load or Save function for all of your game data and not have to write many duplicate lines of code for every piece of data you want to save.

  2. OnGUI is old and bad. It’s not related to your problem here but you should learn to use the modern “Canvas” based system. Check out any UI tutorial from the last ~5 years.