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);
}
}