Object reference not set to an instance of an object

i would like some help on this error:

NullReferenceException: Object reference not set to an instance of an object

NameTransfer.Update () (at Assets/NameTransfer.cs:15)

UnityEngine.Events.InvokableCall.Invoke () (at <9674024ab0e74d27bbe9eaa30dab34d1>:0)

UnityEngine.Events.UnityEvent.Invoke () (at <9674024ab0e74d27bbe9eaa30dab34d1>:0)

UnityEngine.UI.Button.Press () (at C:/Program Files/Unity/Hub/Editor/2019.4.14f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:68)

UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/Program Files/Unity/Hub/Editor/2019.4.14f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:110)

UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/Program Files/Unity/Hub/Editor/2019.4.14f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:50)

UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/Program Files/Unity/Hub/Editor/2019.4.14f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:261)

UnityEngine.EventSystems.EventSystem:Update() (at C:/Program Files/Unity/Hub/Editor/2019.4.14f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:377)

This is my c# code:

using UnityEngine;

using UnityEngine.UI;

public class NameTransfer : MonoBehaviour

{

public string nameOfUser; // typed name by the user

public GameObject inputField;

public GameObject textDisplay;

public void Update()

{

nameOfUser = inputField.GetComponent().text;

textDisplay.GetComponent().text = "Welcome " + nameOfUser + “to the Soil Mechanics Laboratory game!”;

}

}

Object reference not set to an instance of an object means your object is null, or doesn’t have a value. So based on the error being on line 15, either inputField is null or it doesn’t have a Text component assigned on it and your GetComponent call is returning a null value. You’ll have to debug it yourself to figure out which one it is.

2 Likes

Since you named it inputField, chances are you aren’t trying to grab a Text component, but an InputField component.

Also, I’d advise against doing this in Update as it’s a waste of resources. You can grab the value and update the text when they submit their name or if you really want to update the value as they type, just use the OnValueChange event on the InputField.

It’s probably better to reference the components directly BTW. You can assign them exactly the same way in the inspector.

public InputField inputField;
public Text textDisplay;

public string nameOfUser;

void Update()
{
    nameOfUser = inputField.text;
    textDisplay.text = $"Welcome {nameOfUser} to the Soil Mechanics Laboratory game!";
}

As said above, using the OnValueChanged event instead of update would be better than using Update as well.