Unity3D:一旦玩家在无尽的奔跑者中在 z 轴上通过它们,我如何销毁障碍物预制件的实例?

Unity3D: How can I destroy instances of an obstacle prefab once the player passes them on the z-axis in an endless runner?

我已经尝试为实例设置不同的名称,Destroy(this)Destroy(this.gameObject)Destroy(gameObject),但其中 none 似乎有效...

以下是附加到空游戏对象的实例化代码:

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

public class Spawner : MonoBehaviour
{

    public GameObject[] obstaclePatterns;
    public Transform player;
    private float timeBtwSpawn;
    public float startTimeBtwSpawn;
    public float obstacleSpawnDistance;
    public float obstacleSpDMin;
    public float obstacleSpDMax;
    public float decreaseTime;
    public float minTime = 0.65f;
    public float obstacleHeightMin = 10f;
    public float obstacleHeightMax = 15f;

// makes obstacles randomly ahead of player

    private void FixedUpdate() {
        if (timeBtwSpawn <= 0) {
            obstacleSpawnDistance = Random.Range(obstacleSpDMin, obstacleSpDMax);
            int rand = Random.Range(0, obstaclePatterns.Length);
            Instantiate(obstaclePatterns[rand], new Vector3(obstaclePatterns[rand].transform.position.x, Random.Range(obstacleHeightMin, obstacleHeightMax), player.position.z + obstacleSpawnDistance), Quaternion.identity);
            rand = Random.Range(0, obstaclePatterns.Length);
            Instantiate(obstaclePatterns[rand], new Vector3(obstaclePatterns[rand].transform.position.x, Random.Range(obstacleHeightMin, obstacleHeightMax), player.position.z + obstacleSpawnDistance), Quaternion.identity);
            rand = Random.Range(0, obstaclePatterns.Length);
            Instantiate(obstaclePatterns[rand], new Vector3(obstaclePatterns[rand].transform.position.x, Random.Range(obstacleHeightMin, obstacleHeightMax), player.position.z + obstacleSpawnDistance), Quaternion.identity);
            timeBtwSpawn = startTimeBtwSpawn;
            if (startTimeBtwSpawn > minTime) {
                startTimeBtwSpawn -= decreaseTime;
            }
        }
        else {
            timeBtwSpawn -= Time.deltaTime; 
        }
    }
}

下面是障碍物预制件附带的销毁函数的代码:

using UnityEngine;

public class ObjectDestruction : MonoBehaviour {

    public Transform player;

    // Update is called once per frame
    void FixedUpdate()
    {
        if (transform.position.z < player.position.z) {
            Destroy(gameObject);
            Debug.Log("test");
        }
    }
}

Debug.Log 工作正常,但控制台不断产生此错误:MissingReferenceException: The object of type 'GameObject' has been 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. Spawner.FixedUpdate () (at Assets/Scripts/Spawner.cs:26)

编辑:Destroy(this) 似乎摆脱了错误消息,但它并没有破坏对象。

在此先感谢您!

我不熟悉unity3d,但我怀疑当你销毁一个对象时你也需要删除对它的引用:

目前,您有一个存储在 obstaclePatterns 中的游戏对象数组,因此当您销毁该对象时,它变为空,但仍保留在 obstaclePatterns 数组中并且您的代码不断尝试使用它。

有两种方法可以解决这个问题:

  1. 当你销毁一个 GameObject 时,你可以将它从 obstaclePatterns 数组中删除,例如 int x = IndexOf(obstaclePatterns, gameObject); obstaclePatterns[x] = null;
  2. 让你的程序检查 obstaclePatterns 中的 GameObject 是否为 null,如果是,则不要使用它,例如 if(obstaclePatterns[x] != null){do stuff here}.

或者: 您确定要销毁游戏对象吗?再次将对象移动到玩家前面并重新使用它不是更好吗?