首页 > 解决方案 > 我根据教程制作了一个记忆游戏

问题描述

我正在尝试在您可以在此处找到的教程中制作记忆游戏基础

在我得到这个例外之前,一切都很好:

IndexOutOfRangeException:数组索引超出范围。GameController.addgamepuzzles () (在 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呼叫游戏管理器。我还有一个名为 Puzzle Field 的面板,上面附有网格布局元素。我还有一个带有按钮和图像脚本的预制件。

标签: c#unity3d

解决方案


好吧事实上,在我关注所有代码之后很容易找到错误,错误是因为我在填充列表之前完成了游戏拼图,我只需要反转行 addgamepuzzles(); 并将其放在拼图 = Resources.LoadAll ("Sprites/Candy"); 像例子一样

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);
}
}

推荐阅读