NullReferenceException:Object reference not set to an instance of an object BoxScript.OnPointerClick

NullReferenceException: Object reference not set to an instance of an object
BoxScript.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Assets/Scripts/BoxScript.cs:15)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/Program Files/Unity/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/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update() (at C:/Program Files/Unity/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:377

script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class BoxScript : MonoBehaviour, IPointerClickHandler
{
public int cost;
private Main ms;
[SerializeField] private List _items;
public void OnPointerClick(PointerEventData eventData)
{
if (ms.cash >= cost)
{
Instantiate(_items[Random.Range(0, _items.Count)], transform.position, Quaternion.identity);
ms.cash -= cost;
}
else
{

}
}
}

Your “ms” object is null. If “Main” is a script, you have to do following:
ms = GetComponent<Main>();

Please have a look here for proper usage of code tags:

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

Please use code tags: Using code tags properly