Search Unity

Need help with NullReferenceException

Discussion in 'Scripting' started by Unknowner, Jul 20, 2017.

  1. Unknowner

    Unknowner

    Joined:
    Jul 9, 2017
    Posts:
    10
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class abrirPuerta : MonoBehaviour
    7. {
    8.  
    9.     public Text abrirPuertaText;
    10.     public Animator puerta;
    11.     private bool show;
    12.     public AudioClip button;
    13.     public AudioClip door;
    14.     private AudioSource audioSetup;
    15.    
    16.     void Start()
    17.     {
    18.         abrirPuertaText.text = "";
    19.         show = false;
    20.         puerta.SetBool("on", false);
    21.         audioSetup = GetComponent<AudioSource>();
    22.     }
    23.  
    24.     private void Update()
    25.     {
    26.         if (show)
    27.         {
    28.             abrirPuertaText.text = "Pulsa 'E' para abrir la puerta.";
    29.         }
    30.         else
    31.         {
    32.             abrirPuertaText.text = "";
    33.         }
    34.  
    35.         if (Input.GetKeyDown(KeyCode.E) && show == true && puerta.GetBool("on") == false)
    36.         {
    37.             audioSetup.clip = button;
    38.             audioSetup.Play();
    39.             audioSetup.clip = door;
    40.             audioSetup.Play();
    41.             puerta.SetBool("on", true);
    42.         }
    43.         else
    44.         {
    45.             if (Input.GetKeyDown(KeyCode.E) && show == true && puerta.GetBool("on") == true)
    46.             {
    47.                 audioSetup.clip = door;
    48.                 audioSetup.Play();
    49.                 puerta.SetBool("on", false);
    50.             }
    51.         }
    52.     }
    53.  
    54.     private void OnTriggerEnter(Collider other)
    55.     {
    56.         if (other.tag == "Player")
    57.         {
    58.             show = true;
    59.         }
    60.     }
    61.  
    62.     private void OnTriggerExit(Collider other)
    63.     {
    64.         if (other.tag == "Player")
    65.         {
    66.             show = false;
    67.         }
    68.     }
    69. }
    70.  
    I dragged the animations, text, audio etc and still not working dunno why because it was working but now even with ctrl+z wont work anymore :S

     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Immediately prior to line 18, use debug.log() to print out the name of the GameObject and see if maybe there is another instance of your script on another GameObject.

    Code (csharp):
    1.  Debug.Log( name);
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Well, the error claims abrirPuertaText(line 18 and 32) is null. So make sure you have something in the text field in the inspector. Also, make sure you don't have a second copy of the script in your scene.
     
  4. Unknowner

    Unknowner

    Joined:
    Jul 9, 2017
    Posts:
    10
    Oh all my prefs had the same script attached I didnt know they couldn't all have it :S, they are control panels that open a door in front of them(diferent doors)
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    You can reuse a script if needed, but if you have multiple copies and you are accessing variables, every copy of the script needs to have something in those variables. Filling in the variables on one script through the inspector isn't going to fill in the other copies. Otherwise, you'll need to do null checks to avoid trying to access variables that are null.

    But, if you are trying to reuse parts of the script but don't need other parts, it's probably better to create two scripts at that point. Or look into inheritance.