简体   繁体   English

如何在Unity3D中修复``数组索引超出范围错误''

[英]How To Fix 'Array index is out of range error' In Unity3D

I've been following the Block Breaker section of the course Learn To Code By Making Games (In Unity 4.6.3) on Udemy. 我一直在关注Udemy的“通过制作游戏来编写代码”(在Unity 4.6.3中)课程的“突破块”部分。 I decided to take a slightly different path on the game and challenge myself. 我决定在游戏中采取稍微不同的方法挑战自己。 However I have some issues. 但是我有一些问题。 So the idea is that when a ball hits a collider (named loseCollider) at the bottom of the screen, the player will lose 1 life (of which there are 5). 因此,想法是,当球撞击屏幕底部的对撞机(名为loserCollider)时,玩家将失去1条生命(其中有5条生命)。 As you can see from the screen shot below of my game so far, there are 5 hearts, each with a sprite attached. 从到目前为止我的游戏下面的屏幕截图中可以看到,有5个心,每个心都附加了一个精灵。 What I want is to have the ball hit the loseCollider and change the sprite of the hearts according to how many times the loseCollider was hit. 我想要的是让球击中lostCollider,并根据lostCollider被击打的次数来改变心形。 However I keep getting this error message: 但是我不断收到此错误消息:

错误信息

Here is the code: 这是代码:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class LoseCollider : MonoBehaviour {

private LevelManager levelManager;

public Sprite[] lives;

public GameObject lives1;
public GameObject lives2;
public GameObject lives3;
public GameObject lives4;
public GameObject lives5;

private int amountHit;
private int maxHit = 5;


void Start () {
    levelManager = GameObject.FindObjectOfType<LevelManager>();
}

void OnTriggerEnter2D (Collider2D trigger) {
    print ("Trigger");
}

void OnCollisionEnter2D (Collision2D collision) {
    LoadSprites ();
    print ("Collider");
    amountHit++;
    print (amountHit);
    if(amountHit == maxHit){
        levelManager.LoadLevel("Lose");
    }
}

void LoadSprites () {
    int spriteIndex = 1;

    lives1.GetComponent<SpriteRenderer>().sprite = lives[spriteIndex];

    if(amountHit >= 1){
        print ("Hit 1");
        lives1.GetComponent<SpriteRenderer>().sprite = lives[spriteIndex];
    } else if(amountHit >= 2){
        print ("Hit 2");
        lives2.GetComponent<SpriteRenderer>().sprite = lives[spriteIndex];
    } else if(amountHit >= 3){
        print ("Hit 3");
        lives3.GetComponent<SpriteRenderer>().sprite = lives[spriteIndex];
    } else if(amountHit >= 4){
        print ("Hit 4");
        lives4.GetComponent<SpriteRenderer>().sprite = lives[spriteIndex];
    } else if(amountHit >= 5){
        print ("Hit 5");
        lives5.GetComponent<SpriteRenderer>().sprite = lives[spriteIndex];
    }
}

And the screenshot of the game: 游戏截图: 屏幕截图

From your code: 从您的代码:

int spriteIndex = 1;
lives1.GetComponent<SpriteRenderer>().sprite = lives[spriteIndex];

Have you filled the lives array with something? 您是否在lives充满了某些东西? Trying to access an array with an index >= the size of the array will cause an error like yours. 尝试访问索引> =数组大小的数组会导致类似您的错误。 It's hard to say without knowing what you've filled the lives array with from the editor, but I'd start with setting spriteIndex to 0. 这很难不知道你已经填补了说lives从编辑器阵列,但我会用设置开始spriteIndex为0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM