Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Removing specific GameObjects in an array (Specific = After a certain number)

Discussion in 'Scripting' started by Casse, Dec 20, 2015.

  1. Casse

    Casse

    Joined:
    Jan 26, 2014
    Posts:
    20
    So I have this situation where I want a GameObject to instantiate in an array, that works perfectly well. I'm using UI components so that I can adjust a value on how many of those GameObjects I want in the scene.

    Now I'm having some problems figuring out how to adjust the amount of GameObjects. I've cracked how to instantiate more if the script detects that more needs to be added, the problem is removing a certain amount of them if the new amount is less than how many GameObjects there are in the scene.

    Here's the code for the instantiate:

    Code (CSharp):
    1.  
    2. for (int i = 0; i < amount; i++)
    3.             {
    4.                 GameObject clone = (GameObject)Instantiate(container, new Vector3(container.transform.position.x, container.transform.position.y, container.transform.position.z + i * 4), container.transform.rotation);
    5.                 //uses the containers original positions, but moves it in the z axis + i array position(1,2,3 etc) * 4 to create a line of clones
    6.                 containerArray[i] = clone;
    7.             }
    8.  
    and instantiate more if the new amount is more than what exists in the scene:

    Code (CSharp):
    1.  
    2. for (int iAdd = 0 + newAmount; iAdd < amount; iAdd++)
    3.                 {
    4.                     GameObject clone = (GameObject)Instantiate(container, new Vector3(container.transform.position.x, container.transform.position.y, container.transform.position.z + iAdd * 4), container.transform.rotation);
    5.                     containerArray[iAdd] = clone;
    6.                 }
    7.                 newAmount = amount;  //reset the amount
    8.  
    I have a solution for destroying all GameObject by using tags, but what I want is to remove a certain amount of clones. Starting from the highest number in the array (to maintain a straight, unobstructed line as seen in the instantiate process).

    I haven't found a solution on the forums and hope that someone can help me, or at least maybe point me in the right direction?
     
    Last edited: Dec 20, 2015
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
  3. GiantBoots

    GiantBoots

    Joined:
    Dec 20, 2015
    Posts:
    1
    1. for (int iSubtract = amount; iSubtract > newAmount; iSubtract--)
    2. {
    3. Destroy(containerArray[iSubtract]);
    4. containerArray[iSubtract] = null;
    5. }
    6. newAmount = amount; //reset the amount
    Like that? That'll destroy the objects, and leave you with an oversized array full of nulls. Or you could make a new array and copy everything that you want to keep.

    (I really need to learn how to format on this forum)
     
  4. Afrodeity

    Afrodeity

    Joined:
    Dec 19, 2015
    Posts:
    12
    What you could do is to parent each instance to a spawn point. Then you can delete them by their child index:
    Code (CSharp):
    1. GameObject p; //SpawnPoint p
    2. GameObject c; //Container c
    3. //Instantiating children until n exist
    4. for(int i = 0, i < n, i++){
    5.     GameObject tmp = (GameObject)Instantiate(c, p.transform.position + 4*i*Vector3.forward, p.transform.rotation);
    6.     tmp.transform.SetParent(p);
    7. }
    8. //Deleting children until n left
    9. for(int i = p.transform.childCount, i >= n, i--){
    10.     Destroy(p.transform.GetChild(i).GameObject);
    11. }
    (EDIT: Code corrections as pointed out by Casse.)
     
    Last edited: Dec 23, 2015
  5. Casse

    Casse

    Joined:
    Jan 26, 2014
    Posts:
    20
    This was the solution I decided to go for, as I had to use the parents positions anyway. Although to make this work you need to add ".gameObject" in the second function so it reads "Destroy(p.transform.GetChild(i).gameObject);

    EDIT: Also "childCount-1" should just be "childCount" and instead use "Destroy(p.transform.GetChild(i-1).gameObject);

    Thanks for all the helpful replies!
     
    Last edited: Dec 22, 2015