Search Unity

NullReference when prefabbutton becomes visible

Discussion in 'Scripting' started by mv_1993, May 31, 2019.

  1. mv_1993

    mv_1993

    Joined:
    Feb 15, 2019
    Posts:
    4
    Hello Guys,

    I get a NullReference in my programm due to a prefabbutton. If I make the prefabbutton visible/active when I start the program it throws this error, and if I make it invisible and create another instance and make that visible while the program is running, it throws the same error.
    Code (CSharp):
    1.  
    2. public void Awake()
    3.     {
    4.         token = (Token) storageHandler.LoadData("token");
    5.         semesterNumber.text = "semester1"; // line 39
    6.     }
    Code (CSharp):
    1. public void AddMoreButton()
    2.     {
    3.         if (lectureInput.text.Length < 1)
    4.         {
    5.             return;
    6.         }
    7.         lectureTitle.text = lectureInput.text;
    8.         creditsTitle.text = creditsInput.text;
    9.         swsTitle.text = swsInput.text;
    10.         GameObject button = (GameObject) Instantiate (prefabButton);
    11.         Button buttonInstance = button.GetComponentInChildren<Button>();
    12.         buttonInstance.onClick.AddListener(() => {  });
    13.         button.transform.SetParent(panel.transform, false);
    14.         button.layer = 5;
    15.         button.SetActive(true);  //line 99
    16.  
    17.         GameObject childButton = (GameObject)Instantiate(deleteButton);
    18.         Button childButtonInstance = childButton.GetComponentInChildren<Button>();
    19.         childButtonInstance.onClick.AddListener(() => { Destroy(button); });
    20.         childButton.transform.SetParent(button.transform, false);
    21.         childButton.layer = 5;
    22.         childButton.SetActive(true);
    23.  
    24.         ClearInputs();
    25.     }

    NullReferenceException: Object reference not set to an instance of an object
    SemesterScript.Awake () (at Assets/Scripts/SemesterPage/SemesterScript.cs:39)
    UnityEngine.GameObject:SetActive()
    SemesterScript:AddMoreButton() (at Assets/Scripts/SemesterPage/SemesterScript.cs:99)
    UnityEngine.EventSystems.EventSystem:Update()
     
    Last edited: May 31, 2019
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Use code tags, and let us know what line the error points to when you doubleclick it.
     
  3. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    As a general rule of thumb, Awake should be used for initializations that can be done entirely within the current script (without accessing any external objects), or using resources that are guaranteed to be available at the time the GameObject and Component are created and Awake is called. If you're needing to access other GameObjects for some part of the initialization process, or other Components on the same GameObject, and the call order is not guaranteed by the Script Execution Order, then you should place those parts in Start instead.

    I would also check to make sure that the semesterNumber reference is actually set in the inspector, assuming that it's an inspector-assigned member (which isn't completely clear from the portion of code you've provided).
     
  4. mv_1993

    mv_1993

    Joined:
    Feb 15, 2019
    Posts:
    4
    It directs me to line 39
     
  5. mv_1993

    mv_1993

    Joined:
    Feb 15, 2019
    Posts:
    4
    The semesterNumber is a Text UI Element where the reference is set in the inspector.
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Which line is line 39? You've pasted only a fragment of your script file, so the line number in your script file doesn't help us locate the error.
     
  7. mv_1993

    mv_1993

    Joined:
    Feb 15, 2019
    Posts:
    4
    Its commented in the code. Line 5 of the first code section.