Search Unity

How to compare child objects of same name and delete the one that has the highest int assigned

Discussion in 'Scripting' started by Madix, Feb 9, 2016.

  1. Madix

    Madix

    Joined:
    Nov 22, 2013
    Posts:
    16
    Hello,

    Hopefully I can explain this properly =)

    I'm placing multiple UI objects within a panel.

    Let's say these UI Objects are called;

    Circle
    Square
    Triangle

    Each of the objects holds an int value. I can place multiple copies of the object in the panel, however each copy increases the value of the next copy.

    So;

    Circle: 1
    Circle: 3
    Circle: 5
    Square: 1
    Triangle: 1

    What I need is when I click on an object in the panel, it compares all the objects of the same name, and deletes the object that has the highest value. So if I clicked on Circle with the value of 1, it would delete the one with value 5.

    How might I go about doing that? I haven't had any luck searching here or on Google, so I apologize if there is an article on this already. Could someone point me in the right direction, or provide an example of the logic of how to compare the values and delete the highest?

    Thank you for your time and assistance.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Each type of child would need a script to hold the value, if you create them with different script by type (i.e. "CircleScript", "TriangleScript", could get fancy and have them inherit from a base script incase you ever want to look up "anything") you can then easily use "GetComponentsInChildren<...>()" to retrieve "all triangles" into an array from the parent.

    So it's probably best to handle this from the containing object, so it needs to expose a function the children can call when they are clicked on. So the UI element calls the parent's function and passes along it's type.

    Sorting the array would be simplest by using the sorting functionality in the "System.Linq" namespace, if you sort the array descending by value you can then just call destroy on the gameobject the first element in array is attached to.
     
  3. iPLEOMAX

    iPLEOMAX

    Joined:
    Feb 16, 2014
    Posts:
    17
    Here you go:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class Shapes : MonoBehaviour {
    6.  
    7.     //Types of shapes
    8.     public enum ShapeType {
    9.         Circle,
    10.         Triangle,
    11.         Rectangle,
    12.         Square
    13.     }
    14.  
    15.     //Structure of each shape item (shape type, value, deleted)
    16.     [System.Serializable]
    17.     public class Shape {
    18.         public ShapeType shape;
    19.         public int value;
    20.         public bool deleted;
    21.  
    22.         public Shape(ShapeType shape, int value) {
    23.             this.shape = shape;
    24.             this.value = value;
    25.             this.deleted = false;
    26.         }
    27.     }
    28.  
    29.     //List we'll perform our operations on and keep track of
    30.     public List<Shape> shapesList = new List<Shape>();
    31.     private ShapeType[] shapeTypes = {ShapeType.Circle, ShapeType.Rectangle, ShapeType.Triangle, ShapeType.Square};
    32.  
    33.     void Start () {
    34.         AddRandomShapes (100, 10); //Now shapesList contains random shapes with its own random value.
    35.     }
    36.  
    37.     public void AddRandomShapes(int amount, int max_value) {
    38.         for (int i = 0; i < amount; i++) {
    39.             shapesList.Add (
    40.                 new Shape(
    41.                     shapeTypes[Random.Range (0, shapeTypes.Length)],
    42.                     Random.Range(0, max_value)
    43.                 )
    44.             );
    45.         }
    46.     }
    47.  
    48.     public void RemoveHighestCommonShape(ShapeType shape) {
    49.         List<Shape> commonShapes = new List<Shape> ();
    50.  
    51.         //Loop and collect correct shapes that are asked to delete
    52.         foreach(Shape sh in shapesList) {
    53.             if(sh.shape != shape) continue;
    54.             if(sh.deleted) continue;
    55.             commonShapes.Add (sh);
    56.         }
    57.  
    58.         //Make sure we have at least one item in the common shapes or dont do anything
    59.         if (commonShapes.Count > 0) {
    60.             //We'll sort the common shapes from highest to lower (b.value - a.value sorting method)
    61.             commonShapes.Sort (delegate(Shape a, Shape b) {
    62.                 return b.value - a.value;
    63.             });
    64.  
    65.             //Mark the first item in the commonShapes for deletion (Meaning the highest valued shape)
    66.             commonShapes[0].deleted = true;
    67.             StartCoroutine (DelayedRemoveShape(commonShapes[0])); //This will delete the shape after 1 second
    68.         }
    69.     }
    70.  
    71.     public IEnumerator DelayedRemoveShape(Shape shape) {
    72.         yield return new WaitForSeconds (1);
    73.         shapesList.Remove (shape);
    74.     }
    75.  
    76.     //Using the old GUI system for example, you can easily populate the list of items in the new Unity GUI.
    77.     void OnGUI () {
    78.         Rect rect = new Rect (20, 20, 300, 24);
    79.         int rows = 0;
    80.  
    81.         //Draw the shape list
    82.         foreach (Shape sh in shapesList) {
    83.             GUI.color = sh.deleted ? Color.cyan : Color.black;
    84.             GUI.Label (rect, sh.shape.ToString() + ": " + sh.value.ToString ());
    85.             rect.y += 20;
    86.             rows++;
    87.  
    88.             //Just to prevent clipping
    89.             if(rows > 25) {
    90.                 rows = 0;
    91.                 rect.x += 100;
    92.                 rect.y = 20;
    93.             }
    94.         }
    95.  
    96.         GUI.color = Color.white;
    97.         rect = new Rect (Screen.width - 200, 20, 150, 30);
    98.  
    99.         //Draw remove buttons
    100.         foreach (ShapeType st in shapeTypes) {
    101.             rect.y += 32;
    102.             if(GUI.Button (rect, "Remove " + st.ToString ())) {
    103.                 RemoveHighestCommonShape(st);
    104.             }
    105.         }
    106.     }
    107. }
    108.  
     
    Last edited: Feb 9, 2016
  4. Madix

    Madix

    Joined:
    Nov 22, 2013
    Posts:
    16
    Thanks for the replies and assistance. I'll give it a shot.

    Thanks!