Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

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>().text;

       textDisplay.GetComponent<Text>().text = "Welcome " + nameOfUser + "to the Soil Mechanics Laboratory game!";


   }




}


Answers

  • An Object is an instance of a Class , it is stored some where in memory. A reference is what is used to describe the pointer to the memory location where the Object resides. The message "object reference not set to an instance of an object" means that you are referring to an object the does not exist or was deleted or cleaned up. It's usually better to avoid a NullReferenceException than to handle it after it occurs. To prevent the error, objects that could be null should be tested before being used.

    if (mClass != null)
    {
     // Go ahead and use mClass
     mClass.property = ...
    }
    else
    {
     // Attempting to use mClass here will result in NullReferenceException
    }
    


Sign In or Register to comment.