- Home /
Destroy created children of objects
I have applied a code to some objects that creates copies of it as children. I am a newbie and would appreciate if someone can tell me how I can delete these before creating new ones (as the game replays). It's causing a lot of lagging with each replay. I need the array to be cleared but more important is to destroy the objects
In my case the objects are B1 and I1 with prefabs of the same names applied on them. Every time the player dies poolObjects.Length will still have 10 value but the clones will be 10 more each time.
The code is:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Parallaxer : MonoBehaviour {
class PoolObject {
public Transform transform;
public bool inUse;
public PoolObject(Transform t) { transform = t; }
public void Use() { inUse = true; }
public void Dispose() { inUse = false; }
}
[System.Serializable]
public struct YSpawnRange {
public float minY;
public float maxY;
}
public GameObject Prefab;
public int poolSize;
public float shiftSpeed;
public float spawnRate;
public YSpawnRange ySpawnRange;
public Vector3 defaultSpawnPos;
public bool spawnImmediate;
public Vector3 immediateSpawnPos;
public Vector2 targetAspectRatio;
float spawnTimer;
PoolObject[] poolObjects;
float targetAspect;
GameManager game;
void Awake() {
Configure();
}
void Start() {
game = GameManager.Instance;
}
void OnEnable() {
GameManager.OnGameOverConfirmed += OnGameOverConfirmed;
}
void OnDisable() {
GameManager.OnGameOverConfirmed -= OnGameOverConfirmed;
}
void OnGameOverConfirmed() {
for (int i = 0; i < poolObjects.Length; i++) {
poolObjects[i].Dispose();
poolObjects[i].transform.position = Vector3.one * 1000;
}
Configure();
}
void Update() {
if (game.GameOver) return;
Shift();
spawnTimer += Time.deltaTime;
if (spawnTimer > spawnRate) {
Spawn();
spawnTimer = 0;
}
}
void Configure() {
//spawning pool objects
targetAspect = targetAspectRatio.x / targetAspectRatio.y;
poolObjects = new PoolObject[poolSize];
for (int i = 0; i < poolObjects.Length; i++) {
GameObject go = Instantiate(Prefab) as GameObject;
Transform t = go.transform;
t.SetParent(transform);
t.position = Vector3.one * 1000;
poolObjects[i] = new PoolObject(t);
}
if (spawnImmediate) {
SpawnImmediate();
}
}
void Spawn() {
//moving pool objects into place
Transform t = GetPoolObject();
if (t == null) return;
Vector3 pos = Vector3.zero;
pos.y = Random.Range(ySpawnRange.minY, ySpawnRange.maxY);
pos.x = (defaultSpawnPos.x * Camera.main.aspect) / targetAspect;
t.position = pos;
}
void SpawnImmediate() {
Transform t = GetPoolObject();
if (t==null) return;
Vector3 pos = Vector3.zero;
pos.y = Random.Range(ySpawnRange.minY, ySpawnRange.maxY);
pos.x = (immediateSpawnPos.x * Camera.main.aspect) / targetAspect;
t.position = pos;
Spawn();
}
void Shift() {
//loop through pool objects
//moving them
//discarding them as they go off screen
for (int i = 0; i < poolObjects.Length; i++) {
poolObjects[i].transform.position += Vector3.right * shiftSpeed * Time.deltaTime;
CheckDisposeObject(poolObjects[i]);
}
}
void CheckDisposeObject(PoolObject poolObject) {
//place objects off screen
if (poolObject.transform.position.x < (-defaultSpawnPos.x * Camera.main.aspect) / targetAspect) {
poolObject.Dispose();
poolObject.transform.position = Vector3.one * 1000;
}
}
Transform GetPoolObject() {
//retrieving first available pool object
for (int i = 0; i < poolObjects.Length; i++) {
if (!poolObjects[i].inUse) {
poolObjects[i].Use();
return poolObjects[i].transform;
}
}
return null;
}
}
I am inclined more towards unloading, perhaps, ins$$anonymous$$d of Destroying. If that can be done.
Answer by $$anonymous$$ · Apr 11, 2020 at 10:51 AM
Do you want to destroy all children, the last, the first?
Does the normal Destroy(gameObject) not work for you?
You could do it like this: for(int i = transform.childCount - 1; i >= 0; i--) { Destroy(transform.getChild(i).gameObject; }
Your answer
Follow this Question
Related Questions
Is it possible to get the width (in unity units) of a GameObject including all of its children? 1 Answer
Recursively get all children 1 Answer
OnDestroy() callback in Editor upon deleting selected GameObject - del key 5 Answers
Will a large array of game objects slow my game down? 2 Answers
NullReference when accessing GameObject in array (C#) 1 Answer