Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Clicking a button to show text in another panel (NullReferenceException)

Discussion in 'Scripting' started by GarrettSoft, Jun 21, 2020.

  1. GarrettSoft

    GarrettSoft

    Joined:
    Jan 14, 2018
    Posts:
    3
    Hello everyone!

    So,
    I'm trying to make a sort of inventory system, where clicking on a button makes the description of the item appear in a side panel.

    However, I can't wrap my head around how to make it work, because for some reason I keep getting a nullReferenceError.

    The code should be relatively simple:
    First, I serialize the panel and declare the TextMeshPro variable, then assign its value in the awake function.
    Then I call the ShowText() function at the click of a button via events.
    Code (CSharp):
    1. public class InventoryDisplay : MonoBehaviour {
    2.     [SerializeField]    // The inventory description panel
    3.     private GameObject descriptionPanel = default;
    4.     private TextMeshProUGUI descriptionText = default;
    5.  
    6.     private void Awake() {
    7.         descriptionText = descriptionPanel.transform.GetChild(0).GetComponent<TextMeshProUGUI>();
    8.     }
    9.  
    10.     public void ShowText() {
    11.         if (descriptionPanel == null) {
    12.             Debug.Log("Description Panel Null");
    13.         }
    14.         if (descriptionText != null) {
    15.             descriptionText.text = "This is the description if the item.";
    16.         } else Debug.Log("Description Text Null");
    17.     }
    18. }
    The problem is, outside the Awake() function both the descriptionPanel and descriptionText result null.


    What am I missing?
    Thank you, Garrett
     
  2. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    You haven't assigned the panel in your code ...and try to get the text on start()
     
  3. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    And check if child 0 have the component
     
  4. GarrettSoft

    GarrettSoft

    Joined:
    Jan 14, 2018
    Posts:
    3
    The panel is serialized, and I assigned it on the inspector.
    Also, shouldn't you use Awake() to inizialize variables?

    It does. Calling the function in Awake() works, calling it via OnClick() gives error.
     
  5. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    Yes you should .
    So if they both been assigned .maybe somewhere in other code might affect it ?as it won't turn null by itself if they have the reference at awake
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    How many copies of this script exist in your scene? Are you sure you're looking at the right one?
     
  7. GarrettSoft

    GarrettSoft

    Joined:
    Jan 14, 2018
    Posts:
    3
    I solved the issue, thank you.

    The problem was that I made the script create the button instance from a prefab, and the prefab was probably not referencing the correct instance of the script (even if there was only one).
    I added a few lines to properly assign the references after creating the buttons.

    Thank you again!