Search Unity

Null Object Reference although the object EXISTS

Discussion in 'Scripting' started by tomcb, Apr 26, 2019.

  1. tomcb

    tomcb

    Joined:
    Apr 3, 2017
    Posts:
    15
    first of all, sorry for my English.
    now, I have a systemfor a password.
    it is made out of 2 scripts: one for the door, and one for the password itself
    there is a third script on the capera that triggers everything
    these are the scripts (with the relevant parts only):
    Camera Script:
    Code (CSharp):
    1. if (hit.collider.tag == "Door" || hit.collider.tag == "Vents")
    2.                 {
    3.                     CrosshairActive();
    4.                     hitObj = hit.collider.gameObject;
    5.                     if (Input.GetKeyDown(KeyCode.E))
    6.                     {
    7.                         if (hitObj.GetComponent<DoorOpen>().lockSystem.password != null)
    8.                             Debug.Log("Pass Exists");
    9.                         else
    10.                             Debug.Log("Pass Does Not Exist");
    11.  
    12.                         if (hitObj.GetComponent<DoorOpen>().lockSystem.password != null) //BUGGED
    13.                         {
    14.                             Debug.Log("Pass Exists");
    15.                             hitObj.GetComponent<DoorOpen>().lockSystem.password.GetComponent<PasswordSystem>().passwordCanvas.gameObject.SetActive(true);
    16.                         }
    17.                         else if (hitObj.GetComponent<DoorOpen>().CheckIfCanOpen())
    18.                             hitObj.GetComponent<DoorOpen>().OpenDoor();
    19.                     }
    20.                        
    21.                 }
    Door Script:
    Code (CSharp):
    1. [Serializable]
    2.     public class LockSystem
    3.     {
    4.         [Header("Assign If Has A Key")]
    5.         public PickableItem keyToOpen;
    6.  
    7.         [Header("Assign if has password")]
    8.         public GameObject password;
    9.  
    10.         [Header("Is Lock Open?")]
    11.         public bool lockOpen = false;
    12.     }
    13.  
    14. public LockSystem lockSystem = new LockSystem();
    15.  
    16. public void OpenDoor()
    17.     {
    18.         controls.isOpen = true;
    19.         //if used a key and not a lock remove key from inventory
    20.         if (CheckForKey() && !GetLockOpen())
    21.         {
    22.             CLA.inventory[index] = null;
    23.             CLA.slotPos[index].gameObject.GetComponent<Image>().sprite = null;
    24.         }
    25.  
    26.         FindObjectOfType<SubtitlesManager>().PlaySubtitles(controls.openSubtitles);
    27.  
    28.         #region Play sounds
    29.         if (gameObject.tag == "Vents")
    30.         {
    31.             FindObjectOfType<AudioManager>().Play("Vents");
    32.  
    33.         }
    34.         else
    35.         {
    36.             PlaySFX(doorSounds.open);
    37.         }
    38.         #endregion
    39.  
    40.         #region Play animation
    41.         if (GetComponent<Animation>() == null)
    42.             anim.SetTrigger("OpenDoor");
    43.         else if(GetComponent<Animation>() != null)
    44.             GetComponent<Animation>().Play();
    45.         #endregion
    46.  
    47.         gameObject.GetComponent<BoxCollider>().enabled = false;
    48.     }
    49.  
    50. bool CheckForPassword()
    51.     {
    52.         Debug.Log(lockSystem.password.GetComponent<PasswordSystem>().GetUnlocked());
    53.         return lockSystem.password.GetComponent<PasswordSystem>().GetUnlocked();
    54.     }
    and the Password Script:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class PasswordSystem : MonoBehaviour
    5. {
    6.  
    7.     public Canvas passwordCanvas;
    8.     public InputField[] inputFields; //List of all input fields involved
    9.  
    10.     [Space(10)]
    11.     public string yourPassword = "11 22 33";
    12.     public string playerPassword = "";
    13.  
    14.     [Space(10)]
    15.     public bool unlocked = false;
    16.  
    17.     public void CheckPassword()
    18.     {
    19.         GetPlayerPass();
    20.         if (playerPassword == yourPassword)
    21.         {
    22.             transform.GetComponentInParent<DoorOpen>().OpenDoor();
    23.             unlocked = true;
    24.         }
    25.     }
    26.  
    27.     public bool GetUnlocked()
    28.     {
    29.         return unlocked;
    30.     }
    31.  
    32.     void GetPlayerPass()
    33.     {
    34.         playerPassword = "";
    35.         foreach (InputField field in inputFields)
    36.         {
    37.             playerPassword += field.text + " ";
    38.         }
    39.     }
    40.  
    41. }
    now when I tryto open the door by pressing E, I check if there is a passwordSystem object applyed in inspector, and there is. But yet it does not work and says the object does not exist
    Any help? if somthing is unclear tell me and I will try my best to explain better
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Since you are dereferencing game objects so many times, it would help if you told you which script was throwing the exception at which line.

    -ch
     
  3. tomcb

    tomcb

    Joined:
    Apr 3, 2017
    Posts:
    15
    the door script at line 52/53 (52 is a debug for 53)
     
  4. tomcb

    tomcb

    Joined:
    Apr 3, 2017
    Posts:
    15
    Ok so I somehow solved it. I changed the type in line 8 of the door script to PasswordSystem and it somehow worked