数组显示null引用的UnityEvent错误
本文关键字:UnityEvent 错误 引用 显示 null 数组 | 更新日期: 2023-09-27 18:28:47
这是代码片段。我做错了什么。
using UnityEngine;
using System.Collections;
using UnityEngine.Events;
public class UnityEventSystemDemo : MonoBehaviour {
UnityEvent[] objUE;
// Use this for initialization
void Start () {
objUE = new UnityEvent[2];
objUE[0].AddListener(CustomEvent);///Null ref at this point
objUE[1].AddListener(CustomEvent);
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0)) {
Debug.Log("left clicked..");
//calling Events
objUE[0].Invoke();
objUE[1].Invoke();
}
}
void CustomEvent() {
Debug.Log("customEvent..");
}
void CustomEvent1()
{
Debug.Log("customEvent1..");
}
}
NullReferenceException:对象引用未设置为object UnityEventSystemDemo.Update()(位于Assets/Scripts/UnityEventSystemDemo.cs:23)
您忘记初始化数组元素。尝试更新这样的Start方法。
objUE = new UnityEvent[2];
objUE[0] = new UnityEvent();
objUE[0].AddListener(CustomEvent);///Null ref at this point
objUE[1] = new UnityEvent();
objUE[1].AddListener(CustomEvent);