Unity实例化错误

发布于 2025-01-28 08:15:52 字数 1919 浏览 4 评论 0原文

I've created a script that should spawn enemies in some time, and it works perfectly. However, for some reason it shows errors, even then it spawns enemies like it should.

代码:

public class Spawner : MonoBehaviour
{

    [SerializeField]
    private GameObject[] meteors;

    public GameObject meteor1;

    private float TimeBetweenSpawns;
    public float StartTimeBetweenSpawn = 1f;

    public float MaxY = 4.0f;
    public float MinY = -4.0f;
    float yPos;
    

    float MinTime = 1f;
    float MaxTime = 5f;

    private Vector3 spawnPos;

    // Start is called before the first frame update

    void Start()
    {
        
    }

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

        

        yPos = Random.Range(MinY, MaxY);
        spawnPos = new Vector3(transform.position.x, yPos, transform.position.z);


        int WhichMeteor = 0;
        int WhichMeteorRandom = Random.Range(1, 10);
        if(WhichMeteorRandom > 8)
        {
            WhichMeteor = 0;
        }
        else
        {
            WhichMeteor = 1;
        }
        Debug.Log(WhichMeteor.ToString());
        if (TimeBetweenSpawns <= 0)
        {
            Instantiate(meteors[0], transform.position, transform.rotation);

            //float Time = Random.Range(1f,5f);

            TimeBetweenSpawns = 1f;
        }
        else
        {
            TimeBetweenSpawns -= Time.deltaTime;
        }
    }

    private void ChangeSpawnTimes()
    {
        
        Debug.Log(Points.points);
    }
}

错误

如果我尝试仅添加1个Metor而不是数组,只需创建gameObject再次向Inspector添加prefab再次显示出错误,但可以按照原样的速度工作,即它及时催生敌人。

Instantiate(meteor1, transform.position, transform.rotation);

我的错误在哪里?

I’ve created a script that should spawn enemies in some time, and it works perfectly. However, for some reason it shows errors, even then it spawns enemies like it should.

Code:

public class Spawner : MonoBehaviour
{

    [SerializeField]
    private GameObject[] meteors;

    public GameObject meteor1;

    private float TimeBetweenSpawns;
    public float StartTimeBetweenSpawn = 1f;

    public float MaxY = 4.0f;
    public float MinY = -4.0f;
    float yPos;
    

    float MinTime = 1f;
    float MaxTime = 5f;

    private Vector3 spawnPos;

    // Start is called before the first frame update

    void Start()
    {
        
    }

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

        

        yPos = Random.Range(MinY, MaxY);
        spawnPos = new Vector3(transform.position.x, yPos, transform.position.z);


        int WhichMeteor = 0;
        int WhichMeteorRandom = Random.Range(1, 10);
        if(WhichMeteorRandom > 8)
        {
            WhichMeteor = 0;
        }
        else
        {
            WhichMeteor = 1;
        }
        Debug.Log(WhichMeteor.ToString());
        if (TimeBetweenSpawns <= 0)
        {
            Instantiate(meteors[0], transform.position, transform.rotation);

            //float Time = Random.Range(1f,5f);

            TimeBetweenSpawns = 1f;
        }
        else
        {
            TimeBetweenSpawns -= Time.deltaTime;
        }
    }

    private void ChangeSpawnTimes()
    {
        
        Debug.Log(Points.points);
    }
}

Error out of bound

If I try to add only 1 metor not an array, just creating GameObject adding prefab to an inspector it again shows an error, but works as it should, i.e. it spawns enemies in a timely manner.

Instantiate(meteor1, transform.position, transform.rotation);

Where is my mistake?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小ぇ时光︴ 2025-02-04 08:15:52

似乎您的代码中有一个数组&amp;您正在尝试访问其不存在的索引之一。

例如:

int[] r;
r[0] = 5
r[1] = 4;

// Error Happens Here
print(r[2]);

错误是因为r [2]为null&amp;未定义。因此,解决方案是在尝试访问该索引之前检查索引是否未零。

Seems that you have an array in your code & you're trying to access one of its indexes that don't exist.

for example:

int[] r;
r[0] = 5
r[1] = 4;

// Error Happens Here
print(r[2]);

The error is because r[2] is null & isn't defined. So the solution is to check if the index is not null before trying to access it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文