Unity中的Spawn系统,摧毁Spawns

Spawn System in Unity, Destroying Spwans

我正在为我的游戏制作生成系统,它会在随机位置生成敌人,但 Unity 告诉我我没有检查对象是否已被销毁。我试图在这里用其他一些主题来解决它,但我做不到。 这是我的代码:

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

public class EnemySpawn : MonoBehaviour {

    public GameObject Enemy2Spawn;
    public float maxWidth;
    public float minWidth;
    public float rateSpwan;
    private float currentRSpawn = 2.0f;
    public int maxInimigo;
    public int Inimigos = 0;

    void Start()
    {
        transform.position = new Vector3(0, 6.03f, 0);
    }

    // Update is called once per frame
    void Update()
    {
        if (currentRSpawn > Time.time & Inimigos<=maxInimigo)
        {
            transform.position = new Vector3(Random.Range(minWidth, maxWidth), transform.position.y, transform.position.z);
            Instantiate(Enemy2Spawn);
            currentRSpawn = currentRSpawn + rateSpwan;
            Inimigos++;
        }

        if (Enemy2Spawn. == null)
        {
            Destroy(this.gameObject);
        }
    }
}

我得到的错误是:

"The object of type 'GameObject' has been already destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object"

嗯,这个脚本中有几处看起来不对劲:

在那之前:对于错误,如果你将Enemy2Spawn设置为场景中的一个对象并且它被销毁然后该值为null然后当它试图实例化时再次,这会导致错误(可以将条件“Enemy2Spawn != null”放在第一个 if 语句中作为修复)

"transform.position = new Vector3(Random.Range(minWidth, maxWidth), transform.position.y, transform.position.z);" 此行更改附加到脚本的对象,它的位置是准确的,这不会影响你要生成的敌人的位置。

"Instantiate(Enemy2Spawn);" 复制变量的值。克隆体继承原体的所有值,因此位置将是原体的位置。

除非您没有设置“Enemy2Spawn”的值,否则它不会变为空,除非不同的脚本更改该值并且它是预制件。因此,“if (Enemy2Spawn == null)”假设该值已设置,不会更改其他位置,并且在这种情况下被设置为预制件,那么该语句永远不会为真。如果该语句变为真,则“Destroy(this.gameObject);”内的行会破坏脚本所附加的对象,因此对我来说似乎适得其反(破坏生成器),但如果这是防止错误的措施,它应该在Start 或 Awake 功能,或者如果使用不同的脚本设置它,只需破坏脚本而不是将变量设置为 null(虽然我真的很怀疑)。

这是一个经过更改的脚本,应该可以解决我所说的问题并满足您的需求

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

public class EnemySpawn : MonoBehaviour {

    public GameObject Enemy2Spawn;
    public float maxWidth;
    public float minWidth;
    public float rateSpwan;
    private float currentRSpawn = 2.0f;
    public int maxInimigo;
    public int Inimigos = 0;

    void Start()
    {
        transform.position = new Vector3(0, 6.03f, 0);
    }

    // Update is called once per frame
    void Update()
    {
        if (currentRSpawn > Time.time & Inimigos<=maxInimigo)
        {
            Instantiate(Enemy2Spawn, new Vector3(Random.Range(minWidth, maxWidth), transform.position.y, transform.position.z), Quaternion.identity);
            currentRSpawn = currentRSpawn + rateSpwan;
            Inimigos++;
        }
    }
}

错误的不同原因,给定的脚本可能会吐出错误,但我不知道它是如何导致它的,所以它必须是由不同的脚本引起的,该脚本导致附加了脚本的对象在执行更新功能之前被销毁。

此外,脚本 API 建议在使用 JS/unityscript

时像下面这样调用 Destroy

UnityEngine.Object.Destroy(this.gameObject);

希望对您有所帮助,如果没有,则需要更多信息

Update 函数中,您正在检查 GameObject 是否为空,这意味着它不存在,然后您正在使用 Destroy() 销毁那个 不存在。相反,您需要检查对象是否存在于生成敌人的 if 语句中。像这样将它添加到 if 语句中,你应该会很好。

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

public class EnemySpawn : MonoBehaviour {

    public GameObject Enemy2Spawn;
    public float maxWidth;
    public float minWidth;
    public float rateSpwan;
    private float currentRSpawn = 2.0f;
    public int maxInimigo;
    public int Inimigos = 0;

    void Start()
    {
        transform.position = new Vector3(0, 6.03f, 0);
    }

    // Update is called once per frame
    void Update()
    {
        if (currentRSpawn > Time.time && Inimigos <= maxInimigo && Enemy2Spawn == null)
        {
            transform.position = new Vector3(Random.Range(minWidth, maxWidth), transform.position.y, transform.position.z);
            Instantiate(Enemy2Spawn);
            currentRSpawn = currentRSpawn + rateSpwan;
            Inimigos++;
        }
    }
}