No Entiendo el Error

NullReferenceException: Object reference not set to an instance of an object
Guardar.GuardarPosicion () (at Assets/Scripts/Guardar.cs:73)
Guardar.Update () (at Assets/Scripts/Guardar.cs:58)

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;

public class Guardar : MonoBehaviour
{


    public float PosX;
    public float PosY;
    public float PosZ;
    public int Vidaa = 100;
    public Slider healthSlider;

    public int Score;
    public TextMeshProUGUI texto;

    public Vector3 Posicion;

    Health Vida;
    ScoreManager Puntuacion;
    Tiempo Tiempo;

    public TextMeshProUGUI Horas;
    public float tiempoHorass = 0f;
    public TextMeshProUGUI Dias;
    public float tiempoDias = 0f;

    Tiempo Numeros;

    private void Awake()
    {
       // Score = ScoreManager.Score;
        Vida = GetComponent<Health>();
        Puntuacion = GetComponent<ScoreManager>();
        Tiempo = GetComponent<Tiempo>();

    }


    void Start()
    {
        CargarDatos();
    }


    void Update()
    {
       


        if (Input.GetKeyDown(KeyCode.F6))
          {
              GuardarPosicion();
          }
          if (Input.GetKeyDown(KeyCode.F7))
          {
              CargarDatos();
          }
    }

        public void GuardarPosicion()
    {
        PlayerPrefs.SetInt("currenthealth", Vida.currenthealth);
        PlayerPrefs.SetFloat("PosicionX", transform.position.x);
        PlayerPrefs.SetFloat("PosicionY", transform.position.y);
        PlayerPrefs.SetFloat("PosicionZ", transform.position.z);
        PlayerPrefs.SetInt("Score", ScoreManager.Score);
        PlayerPrefs.SetFloat("Horas", Tiempo.tiempoHoras);
        PlayerPrefs.SetFloat("Dias", Tiempo.tiempoDia);

        Debug.Log("Datos Guardados Correctamente");
    }
    public void CargarDatos()
    {
       
        PosX = PlayerPrefs.GetFloat("PosicionX");
        PosY = PlayerPrefs.GetFloat("PosicionY");
        PosZ = PlayerPrefs.GetFloat("PosicionZ");
        Vidaa = PlayerPrefs.GetInt("currenthealth");
        ScoreManager.Score = PlayerPrefs.GetInt("Score");
        Tiempo.tiempoHoras = PlayerPrefs.GetFloat("Horas");
        Tiempo.tiempoDia = PlayerPrefs.GetFloat("Dias");

        Tiempo.tiempoHoras = tiempoHorass;
        Tiempo.tiempoDia = tiempoDias;


        texto.text = "" + Score;

        Score = ScoreManager.Score;

        Vida.currenthealth = Vidaa;
        healthSlider.value = Vida.currenthealth;

        Posicion.x = PosX;
        Posicion.y = PosY;
        Posicion.z = PosZ;

        transform.position = Posicion;

        Debug.Log("Datos Cargados Correctamente");
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class Tiempo : MonoBehaviour
{

    public TextMeshProUGUI Horas;
    public float tiempoHoras = 0f;
    public TextMeshProUGUI Dias;
    public float tiempoDia = 0f;



    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        tiempoHoras += Time.deltaTime;
        Horas.text = " " + tiempoHoras.ToString("f0");

        if (tiempoHoras >= 5)
        {
            tiempoHoras = 0;
            tiempoDia += 1;
            Dias.text = " " + tiempoDia;
        }
    }
}

You have most likely not filled out all fields in the inspector for this script’s instance in the scene.

  • find the GameObject that has the script attached

  • look in the inspector: make sure none of the fields say “missing” or “None”

1 Like