0

I am new to Unity and have been following this tutorial. Basically, I am trying to make a button clear a block of text if pressed. I'm pretty sure I followed all of the steps, but I am getting a NullReferenceException error when I click on the button.

What am I missing/doing wrong? Any tips on what to do if I run into this error in the future?

Here's the code that I have, which should be the exact same as in the tutorial:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ButtonControl : MonoBehaviour
{
    public GameObject theText;

    public void ClearText()
    {
        theText.GetComponent<InputField>().text = "";
    }
}

Here's the hierarchy image, the GameObject I created, and my button command.

cupie8
  • 3
  • 3

1 Answers1

0

There are 2 things that could have happened here, both are very easy to fix:

  1. theText is not defined. Go to the inspector (in the Unity Editor, the box at the right of the screen) and find where you attached your script (the ButtonControl one). There should be a box titled "The Text". If it says "none" in that box, it means that you have not provided a reference to the object. Drag and drop the GameObject with your input field into it.

  2. If theText is defined, it means that the GameObject you defined it as does not have an imputfield attached to it. Check that it is the right object (the one that should have the input field). If it is the right object, add an input field component to it.

Toll Gnoll
  • 160
  • 1
  • 6
  • I believe theText is defined; I dragged InputField (which is the text box you can type in) into the the box titled "The Text". I don't quite follow what you mean by the second point though... Are you saying to check that the Button GameObject has the correct object in the "On Click ()" box? – cupie8 Dec 25 '22 at 00:46
  • In the second point, I mean that the GameObject that you dragged into the field "The Text" may not have an "Input Field" component. So if you click your InputField GameObject and go to the inspector to see its components, you should make sure that it contains an "Input Field" component. – Toll Gnoll Dec 25 '22 at 15:42
  • I just looked, and it's in there... The code itself looks right, right? The error should be incorporating the script into Unity – cupie8 Dec 27 '22 at 00:50
  • The code does look right, so I suspect the error is how it is set up in the Unity Editor. Check where you added the script "Button Control" and go to the GameObject you dragged into the box labelled "The Text". Check that said object has an InputField component. If it does not, add one. – Toll Gnoll Dec 27 '22 at 01:50
  • I suspect the error is because the GetCompnent() function is not returning a result: it has not found a component, so it returns null. Then, when you try to get the text of a null object, unity gives you a null reference exception. – Toll Gnoll Dec 27 '22 at 01:57
  • I think the error is also in the GetComponent() function. But my object does have an InputField component and everything; I don't understand why it's unable to find the component. You can find the object under GameObject >> UI >> Input Field - TextMeshPro – cupie8 Dec 27 '22 at 19:24
  • I think I see what the problem is - you are using the Text Mesh Pro Input field in the scene, but looking for a standard Input Field component. Use GetComponent() to get the correct component. Hope this helps! – Toll Gnoll Dec 28 '22 at 01:55