• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by coolamigo · Apr 11, 2020 at 06:57 AM · gameobjectarrayschildrendelete

Destroy created children of objects

I have applied a code to some objects that creates copies of it as c$$anonymous$$ldren. 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

alt text

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 s$$anonymous$$ftSpeed;
 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;

     S$$anonymous$$ft();
     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 S$$anonymous$$ft() {
     //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 * s$$anonymous$$ftSpeed * 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;
 }

}

1.jpg (20.8 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image coolamigo · Apr 11, 2020 at 01:47 PM 0
Share

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by $$anonymous$$ · Apr 11, 2020 at 10:51 AM

Do you want to destroy all c$$anonymous$$ldren, the last, the first?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image coolamigo · Apr 11, 2020 at 01:35 PM 0
Share
avatar image $$anonymous$$ coolamigo · Apr 13, 2020 at 07:41 PM 0
Share

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

198 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GameObject Arrays 0 Answers

My object wont move when I use transform.translate. 2 Answers

Can't set the transform.position of imported objects correctly 0 Answers

Index out of bounds even tho it shouldnt be [Javascript] 1 Answer

Grabbing all Children and components of those children 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges