簡體   English   中英

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)設置不同的名稱,但它們似乎都不起作用......

這是附加到空游戲 object 的實例化代碼:

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; 
        }
    }
}

這是附加到障礙物預制件的銷毀 function 的代碼:

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) 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,但我懷疑當您銷毀 object 時,您還需要刪除對它的引用:

目前,您有一個 gameObjects 數組存儲在obstaclePatterns中,因此當您銷毀 object 時,它會變為 null,但仍保留在obstaclePatterns數組中,並且您的代碼會繼續嘗試使用它。

有兩種方法可以解決此問題:

  1. 當你銷毀一個GameObject時,你可以將它從obstaclePatterns數組中移除,例如int x = IndexOf(obstaclePatterns, gameObject); obstaclePatterns[x] = null; int x = IndexOf(obstaclePatterns, gameObject); obstaclePatterns[x] = null; 或者
  2. 讓您的程序檢查obstaclePatterns模式中的 GameObject 是否為GameObject ,如果是,則不要使用它,例如if(obstaclePatterns[x] != null){do stuff here}

交替:你確定要銷毀游戲object嗎? 再次將 object 移動到播放器前面並重新使用它不是更好嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM