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)