using UnityEngine; using UnityEngine.UI; public class PanelMatchController : MonoBehaviour { public ImagePanelController imagePanelController; public bool[] isPanelMatched = new bool[16]; // 记录每个面板是否与对应的按钮匹配 public Image imageA; // 图片 a 对象 private void Start() { // 订阅面板点击事件 imagePanelController.OnPanelClick += OnPanelClick; } private void OnPanelClick(int index) { // 判断当前面板是否已经匹配 if (isPanelMatched[index]) { return; } // 标记当前面板已经匹配 isPanelMatched[index] = true; // 判断所有面板是否已经匹配 bool allMatch = true; for (int i = 1; i < 17; i++) { if (!isPanelMatched[i]) { allMatch = false; break; } } if (allMatch) { // 显示图片 a imageA.gameObject.SetActive(true); } } }以上代码报错IndexOutOfRangeException: Index was outside the bounds of the array. PanelMatchController.OnPanelClick (System.Int32 index) (at Assets/horse/a1/PanelMatchController.cs:19) ImagePanelController.OnButtonClick (System.Int32 index) (at Assets/horse/a1/ImagePanelController.cs:118) ImagePanelController+<>c__DisplayClass10_0.<Start>b__1 () (at Assets/horse/a1/ImagePanelController.cs:24) UnityEngine.Events.InvokableCall.Invoke () (at <28d694a2939e4826992577f826717d3b>:0) UnityEngine.Events.UnityEvent.Invoke () (at <28d694a2939e4826992577f826717d3b>:0) UnityEngine.UI.Button.Press () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:70) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:114) UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:57) UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272) UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:514)
时间: 2023-07-14 21:13:53 浏览: 89
这个错误是因为数组越界了,可能是因为你的 isPanelMatched 数组的大小只有 16,但是在 OnPanelClick 方法中的 index 参数可能超过了这个范围。你可以检查一下 OnPanelClick 方法中的 index 参数是否正确,如果不正确,可以修改为正确的范围。另外,你可以在 OnPanelClick 方法中先进行一次范围检查,判断 index 是否超出了数组的范围,如果超出了,就不进行后续的操作,避免出现数组越界的错误。
阅读全文