Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    Dismiss Notice

What is the best way to structure this class

Discussion in 'Scripting' started by aubergine, Feb 26, 2011.

  1. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,402
    Probably a very simple question but i lost it at this point.

    I have a class like follows:

    public class Person {
    public string name;
    public List<Person> children = new List<Person>();
    }

    when i instance this class, it creates 1 person and few children then few more children for the previous children then..etc.

    I dont want that, i want children list to be instanced once. So do i have to seperate children list or is there any other way?
     
  2. Cameron860

    Cameron860

    Joined:
    Jun 1, 2009
    Posts:
    764
    public static List<Person> children = new List<Person>();

    Static means the field belongs to the Class, not every instance of that class.

    You can then access Person.children[...] from anywhere in any other script or just children[...] from within an instance of a Person class and they will all point to a single List.
     
  3. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,402
    I meant i want a children list for every person but i dont want a children list for every persons children.
    If i make it a static then wouldnt it mean theres 1 child list at all?

    Anyways i made a childperson class to wrap things up. and it does the job.
     
  4. Antitheory

    Antitheory

    Joined:
    Nov 14, 2010
    Posts:
    549
    I would make a class Person that contains everything common to your Persons. Then I would make classes "Parent" and "Child" derived from Person.
     
  5. Cameron860

    Cameron860

    Joined:
    Jun 1, 2009
    Posts:
    764
    Arr I see.

    Technically, this shouldn't be an issue as long as your constructor/awake/start code does not create more person instances.

    Generally with this kind of a structure you build/populate it from an external class/script so you don't have people creating other people all willy nilly (like the 70s).

    Having empty List<Person>'s won't really cause any issues. The other options is to create a "soft collection" where you have public pointers to other Persons:

    public Person parent = null;
    public Person firstChild = null;
    public Person nextChild = null;

    As long as your code reinforces these references then all should be fine. public properties with custom get/set code works wonders here, you can also implement array access [...] on top of all this, but that's kinda overkill IMO, may as well just use a list.
     
    Last edited: Feb 26, 2011