- Home /
Destroy instance
Hello, I am making a game where a player has to avoid obstacles. As the borders of the level I use a "box" - like geometry that spawns as a list of gameobjects in a row and translates toward the player by y axis. Player's position is not changing by Y axis. So, as soon as the first box moves away from sight of player (camera), I destroy it, remove at index and it should spawn again as the last in the row. The problem is that the compiler is barking. I have: MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. The problem line is : MoveSpawnDestroyBlocks method. Before I've implemented StateMachine approach, it worked as a charm. Not anymore.
Please help me with it. What's wrong here..?
The spawning script it here:
using UnityEngine; using Code.States; using System.Collections.Generic;
public class Spawning : MonoBehaviour {
public GameObject block_PF;
public GameObject player;
public GameObject obstacle_PF;
public GameObject badPU_PF;
public GameObject goodPU_PF;
public GameObject collectablePU_PF;
public List<GameObject> blocks;
private bool readyToSpawnObstacle = false;
private bool readyToSpawnBadPU = false;
private bool readyToSpawnGoodPU = false;
private bool readyToSpawnCollectablePU = false;
public void BlocksStartSpawn()
{
for (int i = 0; i < 5; i++)
{
float y_pos = i * -25;
GameObject clone = Instantiate(block_PF) as GameObject;
clone.transform.position = new Vector3(0, y_pos, 0);
blocks.Add(clone);
}
}
public void MoveSpawnDestroyBlocks(float blockMoveSpeed_)
{
for (int i = 0; i < blocks.Count; i++)
{
blocks[i].transform.Translate(new Vector3(0, blockMoveSpeed_ * Time.deltaTime, 0));
if(blocks[i].transform.position.y >= 25)
{
GameObject clone = Instantiate(block_PF) as GameObject;
clone.transform.position = new Vector3(0, blocks[i].transform.position.y - 100, 0);
blocks.Add(clone);
Destroy (blocks[0]);
blocks.RemoveAt(0);
}
}
}
public void SpawnDestroyObstacles(float obstacleMoveSpeed_)
{
if(readyToSpawnObstacle)
{
float z_rand = Random.Range(-2.0f, 2.0f);
float rand_rot = Random.Range(0.0f, 180.0f);
GameObject clone = Instantiate(obstacle_PF) as GameObject;
clone.transform.position = new Vector3(0, -100, z_rand);
clone.transform.rotation = Quaternion.Euler(0, rand_rot,0);
clone.rigidbody.AddForce(0, obstacleMoveSpeed_, 0, ForceMode.Force);
DestroyObject(clone, 6.0f);
}
}
public void ObstSpawnCounter(float obstacleSpawnDelay_)
{
float obstacleCounter = 0;
obstacleCounter += Time.deltaTime;
if(obstacleCounter >= obstacleSpawnDelay_)
{
readyToSpawnObstacle = true;
obstacleCounter = 0;
}
else
{
readyToSpawnObstacle = false;
}
}
public void SpawnDestroyBadPU(float badPUSpeed_)
{
if(readyToSpawnBadPU)
{
float rand_x_pos = Random.Range(-2.0f, 2.0f);
float rand_z_pos = Random.Range(-2.0f, 2.0f);
GameObject clone = Instantiate(badPU_PF, new Vector3(rand_x_pos, -125, rand_z_pos), Quaternion.identity) as GameObject;
clone.rigidbody.AddForce(new Vector3(0, badPUSpeed_, 0), ForceMode.Force);
clone.transform.Rotate(Time.deltaTime, Time.deltaTime, Time.deltaTime, Space.Self);
readyToSpawnBadPU = false;
DestroyObject(clone, 8.0f);
}
}
public void SpawnDestroyGoodPU(float goodPUSpeed_)
{
if(readyToSpawnGoodPU)
{
float rand_x_pos = Random.Range(-2.0f, 2.0f);
float rand_z_pos = Random.Range(-2.0f, 2.0f);
GameObject clone = Instantiate(goodPU_PF, new Vector3(rand_x_pos, -125, rand_z_pos), Quaternion.identity) as GameObject;
clone.rigidbody.AddForce(new Vector3(0, goodPUSpeed_, 0), ForceMode.Force);
clone.transform.Rotate(Time.deltaTime, Time.deltaTime, Time.deltaTime, Space.Self);
readyToSpawnGoodPU = false;
DestroyObject(clone, 8.0f);
}
}
public void SpawnDestroyCollectablePU(float collectablePUSpeed_)
{
if(readyToSpawnCollectablePU)
{
float rand_x_pos = Random.Range(-2.0f, 2.0f);
float rand_z_pos = Random.Range(-2.0f, 2.0f);
GameObject clone = Instantiate(collectablePU_PF, new Vector3(rand_x_pos, -125, rand_z_pos), Quaternion.identity) as GameObject;
clone.rigidbody.AddForce(new Vector3(0, collectablePUSpeed_, 0), ForceMode.Force);
clone.transform.Rotate(Time.deltaTime, Time.deltaTime, Time.deltaTime, Space.Self);
readyToSpawnCollectablePU = false;
DestroyObject(clone, 8.0f);
}
}
public void PowerupsCounter(float badPUSpawnDelay_, float goodPUSpawnDelay_, float collectablePUSpawnDelay_)
{
float badPUCounter = 0.0f;
float goodPUCounter = 0.0f;
float collectablePUCounter = 0.0f;
badPUCounter += Time.deltaTime;
goodPUCounter += Time.deltaTime;
collectablePUCounter += Time.deltaTime;
if(badPUCounter >= badPUSpawnDelay_)
{
readyToSpawnBadPU = true;
badPUCounter = 0;
}
if(goodPUCounter >= goodPUSpawnDelay_)
{
readyToSpawnGoodPU = true;
goodPUCounter = 0;
}
if(collectablePUCounter >= collectablePUSpawnDelay_)
{
readyToSpawnCollectablePU = true;
collectablePUCounter = 0;
}
}
}
Your answer
Follow this Question
Related Questions
"Destroy assets is not permitted to avoid data loss" 2 Answers
I need to Instantiate a prefab at the same location where a different object was destroyed., 1 Answer
Cannot destroy instantiated objects 2 Answers
Destroying a prefab doesn't remove it from the Hierarchy list 2 Answers
Instantiate problem 1 Answer