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

How to make a script in which makes it's own arrays and/or Lists at runtime using C#?

Discussion in 'Scripting' started by foxter888, May 13, 2014.

  1. foxter888

    foxter888

    Joined:
    May 3, 2010
    Posts:
    530
    Good Afternoon,

    What i'm currently working on is a group of game objects in which they all have glow maps and i turn them on and of, pretty much like a techno house party :).

    Anyhow Imagine i have a bunch of gameobjects in which i want the script to randomly divide it by a number for example 4 and make it's own array in which will add those objects and flip the lights on it.

    so far i'm not sure what this concept actually is, currently seems to be listed as being dynamic array but searching online i just get answers of how to make arrays, same thing if i was using list which would be my preference.

    so the main idea would be at runtime the script pick a number, make that amount of list or arrays then later on get rid of it and do the sequence again.

    I apologize for my lack of explanation.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. foxter888

    foxter888

    Joined:
    May 3, 2010
    Posts:
    530
    thanks do, will see if any of those would be the ones, although i did saw these ones before. what i'm mainly looking for is a script that can create the arrays on it's own without me having to hard type it seems like the generics might be the one, i do see that others call it different terms so wouldn't know exactly what would be the method of the insanity yet.
     
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Usually I would do something like this.

    Now I'm not sure how you get your list of game objects. This just creates an empty list ready to have some game objects added to it.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4.  
    5. public class ExampleClass : MonoBehaviour {
    6.  
    7.   List<GameObject> m_listOfGameObjects;
    8.  
    9.   void Awake() {
    10.     m_listOfGameObjects = new List<GameObject>();
    11.   }
    12.  
    13. }
    14.  
    EDIT: If you want the list to hold anything, you can make it a list of type object, but you'll have to cast the object back to another type every time you access it. You also lose the benefits of strong typing by doing that. It's usually better to make a list of some base class instead of just "object".
     
    Last edited: May 13, 2014
  5. foxter888

    foxter888

    Joined:
    May 3, 2010
    Posts:
    530
    i believe you misunderstood what i'm asking, in here you are just showing how to make a list but in my case it's not whether if i have knowledge of making a list. what i'm looking for is more like what if the script picks a number such as 6, how to make it so that script makes 6 lists on their own? and how to make it so if it did made those 6 or any random number to make it do things like add a random group of gameobjects and check if the other list has or not those same gameobjects and of the sort.

    so pretty much the script deciding on it's own how many lists or arrays to make not learning how to just make a list lol.
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I don't believe there is anything to prevent having a list of lists...
     
  7. Nezabyte

    Nezabyte

    Joined:
    Aug 21, 2012
    Posts:
    110
    If I understand you correctly, you could create a generic function that takes in the number of lists you want to make. Then that function would create a list of lists to return. Then you could do whatever you want with those lists.