Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help with lists and duplicates

Discussion in 'Scripting' started by TVE, Nov 9, 2015.

  1. TVE

    TVE

    Joined:
    Oct 15, 2012
    Posts:
    379
    Hello unity!

    I have been working on a new system and have ran into a problem with my lists, the cleaning and organizing of them. I have been testing for and foreach loop methods and Contains methods to determine duplicates and extract them.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class DuplicateOcc : MonoBehaviour {
    6.  
    7.     public List<GameObject> Duplicates = new List<GameObject>();
    8.     public List<GameObject> NoDuplicates = new List<GameObject>();
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         Duplicates.AddRange (GameObject.FindGameObjectsWithTag ("object"));
    13.         Occlusion ();
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.  
    19.         foreach(GameObject NoDuplicate in NoDuplicates.ToArray())
    20.         {
    21.             if (NoDuplicate == null) {
    22.                 NoDuplicates.Remove(NoDuplicate);
    23.         }
    24.         }
    25.             foreach(GameObject Duplicate in Duplicates.ToArray())
    26.         {
    27.             if (Duplicate == null) {
    28.                 Duplicates.Remove(Duplicate);
    29.             }
    30.         }
    31.  
    32.     }
    33.  
    34.     void Occlusion () {
    35.  
    36.         foreach (GameObject NoDuplicate in Duplicates.ToArray()) {
    37.             if (!NoDuplicates.Contains(NoDuplicate)) {
    38.                 NoDuplicates.Add(NoDuplicate);
    39.                 Duplicates.Remove(NoDuplicate);
    40.             }
    41.         }
    42.     }
    43. }
    This is code I have been playing with and is very broken. I am just posting this to show some of the methods I have toyed with. My end goal is to have a two lists, one that holds all NoDuplicates and one that holds Duplicates...I also need the Duplicates list to obtain any matching original ones in NoDuplicates, which is why Contains is not helping me. So NoDuplicates are only objects that have absolutely no duplicates in scene. I also need the search to be by objects name to see if duplicate names, which I have not found a solution. My wording of this is probably very bad and confusing so I will answer any and all questions, thank you for reading and sharing your ideas!