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

ID of objects entered into dynamically resizable lists

Discussion in 'Scripting' started by Genkidevelopment, Feb 9, 2015.

  1. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    I have three 'float' lists that dynamically change size - Named: 'Attackers', 'Midfielders' and 'Defenders'. GameObjects are assigned dynamically to one of the three lists depending upon game conditions and may duly change lists at any time. Each list is then sorted and the lowest valued float returned. Now, I have all three lists working well and dynamically resizing as required, BUT I need to know what gameObject is producing the lowest float value...

    here is the code I am using for defining what object goes into what list...
    Code (csharp):
    1. if (HShirt2.position.y >= 421)
    2.  {
    3.  Attackers.Add (HomeRabbit2.position.z);
    4.  }
    5.  else
    6.  {
    7.  if ((HShirt2.position.y >= 251) && (HShirt2.position.y <= 420))
    8.  {
    9.  Midfielders.Add (HomeRabbit2.position.z);
    10.  }
    11.  else
    12.  {
    13.  Defenders.Add (HomeRabbit2.position.z);
    14.  }
    15.  
    This script is repeated so as all gameObjects are each individually assigned respectively... How might I go about assigning an ID to go with the float value being sorted?

    Many thanks

    Peace
     
  2. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,066
    Create a basic class that holds a float and an ID (maybe System.Guid) and create a List<ThatClass> instead of a List<float>?
     
  3. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    In such a way that the list is made up of elements that have TWO bits of data, a float and an ID... This sounds like what I need... I am new to coding though and am struggling with these concepts that require a deeper understanding...
     
  4. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Can I use your suggestion given that I never quite know what gameObject will be in what list though?
     
  5. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,066
    Not 100% sure of your overall goal, but it definitely sounds like a List of more complex objects is the way to go. e.g. you could create a class along the lines of:

    Code (csharp):
    1.  
    2. public class ItemData
    3. {
    4.     public string GameObjectName { get; set; }
    5.     public float ZPosition { get; set; }
    6.     public string Guid { get; set; }
    7. }
    8.  
    And even store the GameObject.name if that helps?

    Then create a:

    Code (csharp):
    1.  
    2. List<ItemData> whatever = new List<ItemData>();
    3.  
    Wherever you need them.
     
  6. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Thanks for the detailed response.

    I'm currently so confused :D But not by the example you posted, thanks for that... My trouble is I never know what fields will be in each list!
     
  7. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Thanks for the advices... I have concluded after more learning dictionaries are exactly what I am after. However I have decided upon a different approach. I am going to tag the players instead... Thanks