Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

No Entiendo el Error

Discussion in 'Scripting' started by Nixel2013, Oct 9, 2019.

  1. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    143
    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)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.UI;
    4. using UnityEngine.EventSystems;
    5. using UnityEngine;
    6. using UnityEngine.SceneManagement;
    7. using TMPro;
    8.  
    9. public class Guardar : MonoBehaviour
    10. {
    11.  
    12.  
    13.     public float PosX;
    14.     public float PosY;
    15.     public float PosZ;
    16.     public int Vidaa = 100;
    17.     public Slider healthSlider;
    18.  
    19.     public int Score;
    20.     public TextMeshProUGUI texto;
    21.  
    22.     public Vector3 Posicion;
    23.  
    24.     Health Vida;
    25.     ScoreManager Puntuacion;
    26.     Tiempo Tiempo;
    27.  
    28.     public TextMeshProUGUI Horas;
    29.     public float tiempoHorass = 0f;
    30.     public TextMeshProUGUI Dias;
    31.     public float tiempoDias = 0f;
    32.  
    33.     Tiempo Numeros;
    34.  
    35.     private void Awake()
    36.     {
    37.        // Score = ScoreManager.Score;
    38.         Vida = GetComponent<Health>();
    39.         Puntuacion = GetComponent<ScoreManager>();
    40.         Tiempo = GetComponent<Tiempo>();
    41.  
    42.     }
    43.  
    44.  
    45.     void Start()
    46.     {
    47.         CargarDatos();
    48.     }
    49.  
    50.  
    51.     void Update()
    52.     {
    53.        
    54.  
    55.  
    56.         if (Input.GetKeyDown(KeyCode.F6))
    57.           {
    58.               GuardarPosicion();
    59.           }
    60.           if (Input.GetKeyDown(KeyCode.F7))
    61.           {
    62.               CargarDatos();
    63.           }
    64.     }
    65.  
    66.         public void GuardarPosicion()
    67.     {
    68.         PlayerPrefs.SetInt("currenthealth", Vida.currenthealth);
    69.         PlayerPrefs.SetFloat("PosicionX", transform.position.x);
    70.         PlayerPrefs.SetFloat("PosicionY", transform.position.y);
    71.         PlayerPrefs.SetFloat("PosicionZ", transform.position.z);
    72.         PlayerPrefs.SetInt("Score", ScoreManager.Score);
    73.         PlayerPrefs.SetFloat("Horas", Tiempo.tiempoHoras);
    74.         PlayerPrefs.SetFloat("Dias", Tiempo.tiempoDia);
    75.  
    76.         Debug.Log("Datos Guardados Correctamente");
    77.     }
    78.     public void CargarDatos()
    79.     {
    80.        
    81.         PosX = PlayerPrefs.GetFloat("PosicionX");
    82.         PosY = PlayerPrefs.GetFloat("PosicionY");
    83.         PosZ = PlayerPrefs.GetFloat("PosicionZ");
    84.         Vidaa = PlayerPrefs.GetInt("currenthealth");
    85.         ScoreManager.Score = PlayerPrefs.GetInt("Score");
    86.         Tiempo.tiempoHoras = PlayerPrefs.GetFloat("Horas");
    87.         Tiempo.tiempoDia = PlayerPrefs.GetFloat("Dias");
    88.  
    89.         Tiempo.tiempoHoras = tiempoHorass;
    90.         Tiempo.tiempoDia = tiempoDias;
    91.  
    92.  
    93.         texto.text = "" + Score;
    94.  
    95.         Score = ScoreManager.Score;
    96.  
    97.         Vida.currenthealth = Vidaa;
    98.         healthSlider.value = Vida.currenthealth;
    99.  
    100.         Posicion.x = PosX;
    101.         Posicion.y = PosY;
    102.         Posicion.z = PosZ;
    103.  
    104.         transform.position = Posicion;
    105.  
    106.         Debug.Log("Datos Cargados Correctamente");
    107.     }
    108. }
    109.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class Tiempo : MonoBehaviour
    7. {
    8.  
    9.     public TextMeshProUGUI Horas;
    10.     public float tiempoHoras = 0f;
    11.     public TextMeshProUGUI Dias;
    12.     public float tiempoDia = 0f;
    13.  
    14.  
    15.  
    16.     void Start()
    17.     {
    18.        
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         tiempoHoras += Time.deltaTime;
    25.         Horas.text = " " + tiempoHoras.ToString("f0");
    26.  
    27.         if (tiempoHoras >= 5)
    28.         {
    29.             tiempoHoras = 0;
    30.             tiempoDia += 1;
    31.             Dias.text = " " + tiempoDia;
    32.         }
    33.     }
    34. }
    35.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    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"
     
    Nixel2013 likes this.