NullReferenceException: Object reference not set to an instance of an object (938293)

Hey everyone. I’m following this tutorial:
NOTEPAD | HOW TO MAKE AN APP IN UNITY TUTORIAL [FULL COURSE] (youtube.com)

I’m at the How to Declare Variables chapter (31:44) and I feel like I’ve inputted everything correctly, but I get the error mentioned in the title. It’s meant to make a button clear the text in the InputField. I can play my game, but the clear button does nothing.

I’m new to coding and this is my first time coding, so it’s likely a rookie mistake. GameObject doesn’t autocomplete if that’s a clue as to what’s going on. I just Updated Visual Studio and installed the Unity extension

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 = "";
}

}

And the full error message:
NullReferenceException: Object reference not set to an instance of an object
ButtonControl.ClearText () (at Assets/Scripts/ButtonControl.cs:14)
UnityEngine.Events.InvokableCall.Invoke () (at <17484a9af6b944dea5cd9be4dbb0da2c>:0)
UnityEngine.Events.UnityEvent.Invoke () (at <17484a9af6b944dea5cd9be4dbb0da2c>:0)
UnityEngine.UI.Button.Press () (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:70)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:114)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:57)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272)
UnityEngine.EventSystems.EventSystem:Update() (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:530)

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

What Kurt said. If you don’t find that out, then you’re basically blindly stumbling around. That’s actually a special case of a more general rule when something goes wrong: gather information before you start shooting in the dark.

Looking at your code, I’m guessing two possibilities. Look at the line that is generating the exception. There are only two variables there that are nullable types. The first is theText. The other one isn’t actually visible - it’s the return value of GetComponent(). You called that and then immediately accessed the text property of the returned object. Note that the text property is a string, which isn’t a nullable type, so we’re not going to consider that.

The first one is easy to check. You have it as a public variable, so I assume you’re doing that to put it in the inspector. So for Kurt’s step 2, it will be null if you didn’t drag anything into the inspector. Go check to make sure you dragged something into that slot.

The second one is also easy to check. Go to that object you dragged into that slot, and make sure it has an InputField component. If not, then there isn’t an InputField to get, and that would be why it’s null.

If you don’t have components or inspector slots to check, then you can also find out what’s null with code:

if (theText == null) {
    Debug.LogWarning("theText is null.");
}

I often do that anyway, before any null references happen, so that I know exactly what’s null just by looking in the console. Make sure you put that code somewhere above where the exception happens, or it will never get to it.

Don’t make any assumptions as you check. Click the object in the slot (meaning click right there in the slot in the inspector) and see which one Unity pings. It’s entirely possible you have the wrong object there.

As for the GameObject not autocompleting, that’s a problem you often run into with a new project. If you can see “Miscellaneous files” at the top of your script instead of “Assembly-CSharp”, then you’ve run into the one I usually do with a fresh Unity install. Here’s how to fix it:

  1. Close VS.
  2. Go back to Unity and Edit->Preferences->External tools
  3. Make sure External Script Editor is set to Visual Studio, and not Open by File Extension. Also be careful not to pick Visual Studio Code by mistake - they look similar.
  4. Look at the next line after all the checkboxes and find the “Regenerate project files” button and click it.
  5. Reopen VS by double-clicking one of your scripts and try it out.

That should fix that type of error. That’s the one I’ve run into the most when helping people get started on a fresh Unity install. If that isn’t it, I’m not sure what it is, to be honest.

Thank you for your help everyone. I feel like part of learning how to use unity is learning how to learn how to use unity, and you’ve both been incredibly helpful.

Lol… you’re not wrong. Unity is a BIIIIIIIG piece of knowledge to get to know.

Over time you’ll keep accumulating connections and information and tidbits.

I like this guy’s approach: one tiny “Can I …?” step at a time:

Imphenzia: How Did I Learn To Make Games: