Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Destroy multiple gameObject at runtime?

Tags:

c#

unity3d

In unity3D I am creating and destroying capsule dynamically at run-time. I used space to create capsule and C for destroying.

I want create multiple object and destroy multiple object at time. When I pressed Space multiple times object is creating multiple time it fine.

But the problem is when I pressed C multiple times only one object is destroying. How I can achieve destroying multiple object? One by one.

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class DynamicCreate : MonoBehaviour 
 {     
     public GameObject caps;

     // Update is called once per frame
     void Update () 
     {
          if (Input.GetKeyDown(KeyCode.Space))
          {
               createObject();
          }

          if (Input.GetKeyDown(KeyCode.C))
          {
               destroyObject();
          }
     }

     private void createObject()
     {
          caps = GameObject.CreatePrimitive(PrimitiveType.Capsule);
     }

     public void destroyObject()
     {
          Destroy(caps);
     }
}
like image 506
Every Thing Avatar asked Dec 21 '18 05:12

Every Thing


2 Answers

You can simply create a list of capsules. Declare a list of capsule like under:

List<GameObject> Capsules;
void Start()
{
    Capsules=new List<GameObject>();
}

Add any created capsule to the list:

private void createObject()
{
    caps = GameObject.CreatePrimitive(PrimitiveType.Capsule);
    Capsules.Add(caps);
}

You can then destroy the capsules in the list:

public void destroyObject()
{
    foreach(GameObject capsule in Capsules)
    {
        Destroy(capsule);
    }
}

See if that helps.

like image 55
bolkay Avatar answered Oct 12 '22 08:10

bolkay


I would like to add more to bolkay answer. If you want to add and remove one by one best datastructure to use is a queue.

Queue<GameObject> Capsules;
void Start()
{
    Capsules = new Queue<GameObject>();
}

private void createObject()
{
    GameObject caps = GameObject.CreatePrimitive(PrimitiveType.Capsule);
    Capsules.Enqueue(caps);
}

public void destroyObject()
{
    if(Capsules.Count > 0)
        Destroy(Capsules.Dequeue());   
}

Queue data structure will give a first in first out. So it will give out first gameobject which you added to the queue at runtime and you wont need a variable to manage the index as in the state of List.


Note: This solution is still very basic but I would recommend you to use object pooling instead of creating and destroying objects at runtime as creating and destorying objects is quite intensive at runtime. So you should unless abosolutely necessary avoid creating and deleting.

Queue<GameObject> pooledObjects;
Queue<GameObject> Capsules;
void Start()
{
    pooledObjects = new Queue<GameObject>();

    Capsules = new Queue<GameObject>();
}

private void createObject()
{
    if(pooledObjects.Count > 0)
    {
        GameObject fetchedObject = pooledObjects.Dequeue();
        fetchedObject.SetActive(true);
        //Do translations,scaling or other stuff on this reference

        //Add it back to main list of things present in scene.
        Capsules.Enqueue(fetchedObject);
    } else
    {
        //Only create objects if necessary
        GameObject caps = GameObject.CreatePrimitive(PrimitiveType.Capsule);
        Capsules.Enqueue(caps);
    }            
}

public void destroyObject()
{
    //here instead of destorying the object we will just disable them and keep them out if scene so we dont have to recreate them everytime
    if(Capsules.Count > 0)
    {
        GameObject fetchedObject = Capsules.Dequeue();
        fetchedObject.SetActive(false);//Turn the object off from scene
        pooledObjects.Equals(fetchedObject);
    }   
}

Here is list of supported datastructures in C# you might want to use depending on your required case.

like image 32
killer_mech Avatar answered Oct 12 '22 08:10

killer_mech