Я пробовал установить разные имена для экземпляров и Destroy(this)
, Destroy(this.gameObject)
и только Destroy(gameObject)
, но ни один из них, похоже, не работает ...
Вот код для экземпляров прикрепленный к пустому игровому объекту:
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)
EDIT: Destroy(this)
похоже, избавляется от сообщения об ошибке, но не уничтожает объекты.
Заранее большое спасибо!