Search Unity

I don't understand the mistake that comes out

Discussion in 'Scripting' started by Nixel2013, Aug 8, 2019.

  1. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    143
    I get these 3 errors but I don't understand why, please help
    Error 1: NullReferenceException: Object reference not set to an instance of an object
    Recordatorio.Start () (at Assets/ScriptGuardarCargar/Recordatorio.cs:20)

    Error 2: NullReferenceException: Object reference not set to an instance of an object
    Recordatorio_OnTriggerEnter (UnityEngine.Collider other) (at Assets/ScriptGuardarCargar/Recordatorio.cs:33)

    Error 3: NullReferenceException: Object reference not set to an instance of an object
    Recordatorio_OnTriggerExit (UnityEngine.Collider o) (at Assets/ScriptGuardarCargar/Recordatorio.cs:42)



    Code (CSharp):
    1. [FONT=Helvetica]using System.Collections;[/FONT]
    2. using System.Collections.Generic;
    3. using UnityEngine.UI;
    4. using UnityEngine;
    5.  
    6. public class Recordatorio : MonoBehaviour
    7. {
    8.  
    9.   //  bool active;
    10.  //   Canvas canvas;
    11.     public Image Imagen;
    12.  
    13.     void Start()
    14.     {
    15.  
    16.    //     canvas = GetComponent<Canvas>();
    17.      //   canvas.enabled = false;
    18.  
    19.         Imagen = GetComponent<Image>();
    20.         Imagen.enabled = false;
    21.        
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.  
    27.     }
    28.  
    29.     void OnTriggerEnter(Collider other)
    30.     {
    31.         if (other.CompareTag("Player"))
    32.          //   Imagen = GetComponent<Image>();
    33.             Imagen.enabled = true;
    34.             Debug.Log("Entraste!");
    35.     }
    36.  
    37.     void OnTriggerExit(Collider o)
    38.     {
    39.         if (o.CompareTag("Player"))
    40.         {
    41.         //    Imagen = GetComponent<Image>();
    42.             Imagen.enabled = false;
    43.             Debug.Log("saliste");
    44.  
    45.         }
    46.     }
    47. }
    48.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,738
    Did you drag an Image instance into the field on line 11?

    If so, then REMOVE line 19 because that would replace it with whatever Image is found on the current GameObject, which might be none.
     
  3. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    143
    Thanks bro, i'm noob in this theme
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,738
    You're welcome! Keep at it and one day you'll look up and smile and say "I'm not a noob anymore!"
     
    Nixel2013 likes this.