这是代码段。我做错了什么。
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:未将对象引用设置为的实例 对象UnityEventSystemDemo.Update()(at 资产/脚本/ UnityEventSystemDemo.cs:23)
答案 0 :(得分:1)
您忘记初始化数组元素。尝试更新这样的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);