我根据一个教程制作了一个记忆游戏



我正在尝试在教程中制作一个内存游戏库,你可以在这里找到

一切都很顺利,直到我遇到这个例外:

IndexOutOfRangeException:数组索引超出范围。GameController.addgameguises(((位于Assets/memorygame/scripts/GameController.cs:43(

这是行

gamepuzzles.Add(puzzles[index]);

这是我的实际代码:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class GameController : MonoBehaviour {
[SerializeField]
private Sprite bgimage;
public Sprite[] puzzles;
public List<Sprite> gamepuzzles = new List<Sprite>();
public List<Button> btns = new List<Button>();

void Start () {
GetButttons();
AddListeners();
addgamepuzzles();
puzzles = Resources.LoadAll<Sprite> ("Sprites/Candy");
}
void GetButttons()
{
GameObject[] objects = GameObject.FindGameObjectsWithTag("PuzzleButton");
for(int i = 0; i < objects.Length; i++){
btns.Add(objects[i].GetComponent<Button>());
btns[i].image.sprite = bgimage;
}
}
void addgamepuzzles(){
int looper = btns.Count;
int index = 0;
for (int i = 0; i < looper; i++){
if (index == looper / 2){
index = 0;
}
gamepuzzles.Add(puzzles[index]);
index++;
}
}
void AddListeners(){
foreach (Button btn in btns){
btn.onClick.AddListener(() => PickPuzzle());
}
}
public void PickPuzzle(){
string name = UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject.name;
Debug.Log("you clicked a button with name: " + name);
}
}

这是我的添加按钮脚本

using UnityEngine;
using System.Collections;
public class AddButtons : MonoBehaviour {
[SerializeField]
public Transform PuzzleFiled;
[SerializeField]
private GameObject btn;

void Awake () {
for(int i = 0; i < 8; i++){
GameObject button = Instantiate(btn);
button.name ="" +i;
button.transform.SetParent(PuzzleFiled, false);
}
}
}

这两个脚本被附加到一个空的gameObject呼叫游戏管理器。此外,我有一个名为PuzzleField的面板,上面附加了网格布局元素。我还有一个预制的按钮和图像脚本

您的index大于您的谜题数组的大小。我想问题发生在你填充数组时:

puzzles = Resources.LoadAll<Sprite>("Sprites/Candy");

你确定这是正确的目录吗?如果它不存在、为空或拼写错误,那么函数将不返回任何内容,并且你的数组将保持为空

事实上,这个错误对我来说很容易找到,让我了解所有代码,这个错误是因为我在填充列表之前玩得很好,我只需要反转addgamepluzles((行;并将其放在谜题后面=资源。LoadAll("精灵/糖果"(;就像的例子

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class GameController : MonoBehaviour {
[SerializeField]
private Sprite bgimage;
public Sprite[] puzzles;
public List<Sprite> gamepuzzles = new List<Sprite>();
public List<Button> btns = new List<Button>();

void Start () {
GetButttons();
AddListeners();
puzzles = Resources.LoadAll<Sprite> ("Sprites/Candy");
addgamepuzzles();
}
void GetButttons()
{
GameObject[] objects = GameObject.FindGameObjectsWithTag("PuzzleButton");
for(int i = 0; i < objects.Length; i++){
btns.Add(objects[i].GetComponent<Button>());
btns[i].image.sprite = bgimage;
}
}
void addgamepuzzles(){
int looper = btns.Count;
int index = 0;
for (int i = 0; i < looper; i++){
if (index == looper / 2){
index = 0;
}
gamepuzzles.Add(puzzles[index]);
index++;
}
}
void AddListeners(){
foreach (Button btn in btns){
btn.onClick.AddListener(() => PickPuzzle());
}
}
public void PickPuzzle(){
string name = UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject.name;
Debug.Log("you clicked a button with name: " + name);
}
}

谜题在添加到其中之前必须有定义数量的元素。我认为你必须做公共的Sprite[]谜题=新的Sprite[这里的元素数量];

最新更新