Search Unity

Bug I keep receiving errors when I look at certain objects with a script on them. Please help.

Discussion in 'Scripting' started by EvelynShilosky, Jan 26, 2023.

  1. EvelynShilosky

    EvelynShilosky

    Joined:
    Sep 13, 2022
    Posts:
    1
    When trying to get the object name by looking at an object with my interactable script I get this error: "NullReferenceException: Object reference not set to an instance of an object SelectionManager.Update () (at Assets/Scripts/SelectionManager.cs:35).

    The code for the line it is saying I am having a problem is is... interaction_text.text = selectionTransform.GetComponent<InteractableObject>().GetItemName();

    If anyone has any idea on how to fix this please help

    The full Selection Manager script is:

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;

    5. public class SelectionManager : MonoBehaviour
    6. {


    7. public GameObject interaction_Info_UI;
    8. Text interaction_text;

    9. private void Start()
    10. {
    11. interaction_text = interaction_Info_UI.GetComponent<Text>();
    12. }





    13. void Update()
    14. {
    15. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    16. RaycastHit hit;
    17. if (Physics.Raycast(ray, out hit))
    18. {
    19. var selectionTransform = hit.transform;


    20. if (selectionTransform.GetComponent<InteractableObject>())
    21. {

    22. interaction_text.text = selectionTransform.GetComponent<InteractableObject>().GetItemName();
    23. interaction_Info_UI.SetActive(true);
    24. }
    25. else //if there is a hit, but without an Interactable Script.
    26. {

    27. interaction_Info_UI.SetActive(false);

    28. }

    29. }
    30. else //if there is no hit at all.
    31. {
    32. interaction_Info_UI.SetActive(false);
    33. }



    34. }

    35. }
    The full Interactable Objects script is:
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;

    4. public class InteractableObject : MonoBehaviour
    5. {

    6. public bool playerInRange;

    7. public string ItemName;




    8. public string GetItemName()
    9. {

    10. return ItemName;
    11. }


    12. private void OntTriggerEnter(Collider other)
    13. {
    14. if (other.CompareTag("Player"))
    15. {

    16. playerInRange = true;


    17. }

    18. }




    19. private void OntTriggerExit(Collider other)
    20. {
    21. if (other.CompareTag("Player"))
    22. {

    23. playerInRange = false;


    24. }



    25. }







    26. }
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,750
    You NEVER have to post for this error... the answer is ALWAYS the same. ALWAYS!

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that