UnityAction等のdelegateのnullチェックをしないで使ってしまいよく発生するエラー
Object reference not set to an instance of an object
null 条件演算子でチェックする
public class ListItem: MonoBehaviour, IPointerClickHandler { public UnityAction<int> OnClick; public int Id { get; private set; } public void OnPointerClick(PointerEventData eventData) { OnClick?.Invoke(Id); } }
null 条件演算子メンバー アクセスと null 条件演算子と式: - C# | Microsoft Learn
・環境
Unity 2022.3.11f1
Project Settings
Api Compatibility Level* : .NET Framework