简体   繁体   English

在 Unity 2D 中生成敌人

[英]Spawning enemies in Unity 2D

I wanted to make my enemies appear from top using 'spawn' while rotating on itself.我想让我的敌人在旋转时使用“spawn”从顶部出现。

But I received this error:但我收到了这个错误:

IndexOutOfRangeException: Array index is out of range. IndexOutOfRangeException: 数组索引超出范围。 spawnScript.addEnemy () (at Assets/Scripts/spawnScript.cs:21) spawnScript.addEnemy () (在 Assets/Scripts/spawnScript.cs:21)

Below is my script:下面是我的脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class spawnScript : MonoBehaviour {

public Transform[] spawnPoints;
public GameObject enemy;
public float spawnTime = 5f;
public float spawnDelay = 3f; 

// Use this for initialization
void Start () {
    InvokeRepeating ("addEnemy", spawnDelay, spawnTime);

}

void addEnemy() {
    // Instantiate a random enemy.
    int spawnPointIndex = Random.Range(0, spawnPoints.Length);
    Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
}

// Update is called once per frame
void Update () {

}
}

Here is the problem: public Transform[] spawnPoints;这是问题所在: public Transform[] spawnPoints;

The spawnPoints variable is declared as public which means that you want to fill it up through the Editor. spawnPoints变量被声明为 public,这意味着您希望通过编辑器填充它。 You failed to do that and the size is still 0 .你没有做到这一点,大小仍然是0 When the size is 0 , Random.Range will do this Random.Range(0,0) and will return 0 .当大小为0Random.Range将执行此Random.Range(0,0)并返回0 When you feed 0 as index to spawnPoints variable, it will throw that error because there is nothing in that spawnPoints .当您将0作为索引提供给spawnPoints变量时,它会抛出该错误,因为spawnPoints没有任何spawnPoints You must set the size.您必须设置大小。

This is what it looks like now:这是现在的样子:

在此处输入图片说明

This is what it is supposed to look like:这是它应该是什么样子:

在此处输入图片说明

Notice how I dragged transforms to the spawnPoints array slots on my second screenshot.请注意我如何将变换拖动到第二个屏幕截图中的spawnPoints数组插槽。 If you don't do that, expect to get NullException error.如果你不这样做,预计会得到NullException错误。

If you don't want to get that error without seting the size then check if spawnPoints.Length > 0 before using it.如果您不想在不设置大小的情况下出现该错误,请在使用之前检查spawnPoints.Length > 0

if (spawnPoints.Length > 0)
{
    int spawnPointIndex = UnityEngine.Random.Range(0, spawnPoints.Length);
    Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
}

By making the spawnPoints a public it is assumed that you want to set the size from the Editor.通过将spawnPoints public ,假定您要从编辑器设置size You can also set the size from script but make it a private variable first so that you won't run into issues:您还可以从脚本设置size ,但首先将其设为private变量,以免遇到问题:

void Start()
{
    //Set the size to 3 then fill it up
    spawnPoints = new Transform[3];
    spawnPoints[0] = yourPint1;
    spawnPoints[1] = yourPint2;
    spawnPoints[2] = yourPint3;
}

Error here - int spawnPointIndex = Random.Range(0, spawnPoints.Length);这里的错误 - int spawnPointIndex = Random.Range(0, spawnPoints.Length);

You should write - Random.Range(0, spawnPoints.Length - 1)你应该写 - Random.Range(0, spawnPoints.Length - 1)

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

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