I've tried setting different names for the instantiations and Destroy(this)
, Destroy(this.gameObject)
, and just Destroy(gameObject)
but none of them seem to work...
Here is the code for the instantiations attached to an empty game 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;
}
}
}
And here is the code for the destroy function attached to the obstacle prefab:
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");
}
}
}
The Debug.Log
works fine, but the console keeps producing this error: 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)
seems to get rid of the error message, but it does not destroy the objects.
Thank you so much in advance!
I am not familiar with unity3d, but I suspect that when you destroy an object you also need to remove references to it:
Currently, you have an array of gameObjects stored in obstaclePatterns
, so when you destroy the object it becomes null, but remains in the obstaclePatterns
array and your code keeps trying to use it.
There are two ways to fix this:
GameObject
you could remove it from the obstaclePatterns
array, for example int x = IndexOf(obstaclePatterns, gameObject); obstaclePatterns[x] = null;
orGameObject
in obstaclePatterns
is null, and if so, then do not use it, for example if(obstaclePatterns[x] != null){do stuff here}
.Alternately: Are you sure that you want to destroy the game object? Wouldn't it be better to just move the object ahead of the player again and re-use it?