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. Dismiss Notice

How can I destroy gameobjects from a list ?

Discussion in 'Scripting' started by Chocolade, Apr 18, 2020.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    Mono :

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5. using System.Collections.Generic;
    6.  
    7. public class GenerateUI : MonoBehaviour
    8. {
    9.     public GameObject imagePrefab;
    10.     public int gap = 1;
    11.  
    12.     private List<GameObject> imagesList = new List<GameObject>();
    13.  
    14.     public void CreateImages(int numOfImages)
    15.     {
    16.         for (int i = 0; i < numOfImages; i++)
    17.         {
    18.             GameObject image = Instantiate(imagePrefab, transform);
    19.             image.name = "Image";
    20.             RectTransform rt = image.GetComponent<RectTransform>();
    21.             image.transform.localPosition = new Vector3(
    22.                 image.transform.localPosition.x + i * (rt.rect.width + gap),
    23.                 image.transform.localPosition.y,
    24.                 image.transform.localPosition.z);
    25.  
    26.             imagesList.Add(image);
    27.         }
    28.     }
    29.  
    30.     public void DestroyImages(int numberToDestroy)
    31.     {
    32.         if (imagesList.Count > 1)
    33.         {
    34.             for (int i = 0; i < numberToDestroy; i++)
    35.             {
    36.                 DestroyImmediate(imagesList[i]);
    37.             }
    38.         }
    39.     }
    40. }
    41.  
    Editor :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEditor;
    5. using UnityEngine;
    6.  
    7. [CustomEditor(typeof(GenerateUI))]
    8. public class GenerateUIEditor : Editor
    9. {
    10.     private static float scale = 0.0f;
    11.     private static int oldVal = 0;
    12.  
    13.     public override void OnInspectorGUI()
    14.     {
    15.         DrawDefaultInspector();
    16.  
    17.         GenerateUI myscript = (GenerateUI)target;
    18.  
    19.         scale = EditorGUILayout.Slider((int)scale, 1, 100);
    20.  
    21.         if (oldVal > (int)scale)
    22.         {
    23.             myscript.CreateImages((int)scale);
    24.         }
    25.  
    26.         if(oldVal < (int)scale)
    27.         {
    28.             myscript.DestroyImages((int)scale - oldVal);
    29.         }
    30.  
    31.         oldVal = (int)scale;
    32.     }
    33. }
    34.  
    Now it's opposite. when I'm moving the slider ui in the editor script to the left it's creating new ui images and when moving to the right it does nothing.

    And I want that when moving the slider to the left destroy existing ui images gameobjects and to the right create new.
    The minimum value of the slider on the left is 1 maximum on the right is 100
     
  2. voncarp

    voncarp

    Joined:
    Jan 3, 2012
    Posts:
    187
    I think you want to do it in reverse. And you want to remove the object from the list before you destroy it.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using System.Collections.Generic;
    5.  
    6. public class GenerateUI : MonoBehaviour
    7. {
    8.     public GameObject imagePrefab;
    9.     public int gap = 1;
    10.  
    11.     public List<GameObject> imagesList = new List<GameObject>();
    12.  
    13.     public void CreateImages(int numOfImages)
    14.     {
    15.         for (int i = 0; i < numOfImages; i++)
    16.         {
    17.             GameObject image = Instantiate(imagePrefab, transform);
    18.             image.name = "Image";
    19.             RectTransform rt = image.GetComponent<RectTransform>();
    20.             image.transform.localPosition = new Vector3(
    21.                 image.transform.localPosition.x + i * (rt.rect.width + gap),
    22.                 image.transform.localPosition.y,
    23.                 image.transform.localPosition.z);
    24.  
    25.             imagesList.Add(image);
    26.         }
    27.     }
    28.  
    29.     public void DestroyImages(int numberToDestroy)
    30.     {
    31.         if (imagesList.Count > 1)
    32.         {
    33.             GameObject objectToDestroy = null;
    34.             for (int i = imagesList.Count - 1; i > numberToDestroy; i--)
    35.             {
    36.                 objectToDestroy = imagesList[i];
    37.                 imagesList.RemoveAt(i);
    38.                 DestroyImmediate(objectToDestroy);
    39.             }
    40.         }
    41.     }
    42. }
    And maybe flip the create to destroy.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [CustomEditor(typeof(GenerateUI))]
    5. public class GenerateUIEditor : Editor
    6. {
    7.     private static float scale = 0.0f;
    8.     private static int oldVal = 0;
    9.  
    10.     public override void OnInspectorGUI()
    11.     {
    12.         DrawDefaultInspector();
    13.  
    14.         GenerateUI myscript = (GenerateUI)target;
    15.  
    16.         scale = EditorGUILayout.Slider((int)scale, 1, 100);
    17.  
    18.         if (oldVal > (int)scale)
    19.         {
    20.             myscript.DestroyImages((int)scale);
    21.             //myscript.CreateImages((int)scale);
    22.         }
    23.  
    24.         if (oldVal < (int)scale)
    25.         {
    26.             //myscript.DestroyImages((int)scale - oldVal);
    27.             myscript.CreateImages((int)scale - oldVal);
    28.         }
    29.  
    30.         oldVal = (int)scale;
    31.     }
    32. }
    33.  
     
    Chocolade likes this.
  3. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    Almost working but if I move the slider back to the left to minimum 1 there are 2 in the list and not 1. Also in the hierarchy there is 2 objects left and not 1.

    And when moving the slider to the maximum to the right to 100 there is 101 objects maybe the 101 object is also the prefab that count in the list ?

    I think when moving to the left to 1 there should be left two objects in the hierarchy the prefab and the 1 since it's moving to 1 not to 0 also in the list in the script there should be 1 since the list is according to the slider value and should not account the prefab but not sure about this logic.

    And to the right to 100 if I'm at 1 and move to the right or at any value and moving to 100 there should be 100 in the list not 101.