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

Issues understanding and implementing dynamic List/Array

Discussion in 'Scripting' started by fr0stiee, Jun 17, 2014.

  1. fr0stiee

    fr0stiee

    Joined:
    May 10, 2013
    Posts:
    3
    Hello!

    I am trying to teach myself coding and how to implement it into games at the same time and I am currenty having issues understanding the use of list<>

    I am trying to make a 2d shooter, similar to Risk of Rain, and I have been watching Brackeys on youtube. I have watched and implemented the parallaxing background and background creation as is it comes into view from the camera.

    My issue is getting the newly created backgrounds and storing it in an array or list and using that alongside the parallaxing but I can't figure out a good way to store it.

    1. myList = GameObject.FindGameObjectsWithTag("Background");
    this doesn't work because it comes up with an error saying this isn't an array.

    So I tried:

    1. backgrounds = GameObject.FindGameObjectsWithTag("Background");
    2. foreach(GameObject background in backgrounds)
    3. {
    4. myList.Add(background);
    5. }
    Which works, but in the Update function just stores the same information repeatedly.

    And doesn't this also delete the need to have a list?

    Any help would be greatly appreciated as I cannot wrap my head around this.

    Thank you in advance,

    Ben
     
  2. Pati-Co

    Pati-Co

    Joined:
    Jan 9, 2014
    Posts:
    56
    It occures because Update() function calls by Unity Engine every frame. If you will fill you array in Start() function it will fill only once.
     
  3. Pati-Co

    Pati-Co

    Joined:
    Jan 9, 2014
    Posts:
    56
    Using of arrays is ok too. Eventualy, now for you there is only one difference between lists and arrays - array has a fixed number of items, and lists could be populated (and "un-populated") at any time.
    Bellow example with array:
    Code (csharp):
    1. GameObject[] myArray = GameObject.FindGameObjectsWithTag("Background");
     
  4. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    To fill your list in one line you need to use List's special constructor. Like this:
    Code (CSharp):
    1.     void Start () {
    2.         List<GameObject> MyList = new List<GameObject> (GameObject.FindGameObjectsWithTag ("test_tag"));
    3.         foreach (GameObject go in MyList) Debug.Log (go.name);
    4.     }
    And of course you only need to populate your list once, not every frame. Later in game you can add newly instantiated object in list without recreation of whole list.

    It doesn't matter if you use list, array, dictionary or whatever else. As long as you don't search objects by name or tag all the time. That's the point of using collections.
     
    Last edited: Jun 17, 2014
  5. fr0stiee

    fr0stiee

    Joined:
    May 10, 2013
    Posts:
    3
    I need to be able to get each new background that is being created by the tiling script, it creates a new one as the camera reaches the end of each backgrounds x position. So having it in the start function wouldn't be what I need :/


    Oh lord:

    Code (CSharp):
    1. myList = new List<GameObject>(GameObject.FindGameObjectsWithTag("Background"));
    This is awesome, thanks!

    As for picking up the newly created backgrounds, how would/should I go about doing that? As I said above, the tiling script is checking if the ortho camera is reaching the end of the background sprite and is making a new one directly next to that?
     
  6. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    Keep your list as a public variable in a GameMaster object or static class or whatever else you use as a head controller for your game. And tell your method that is generating tiles to add tile to this list every time it makes one.
     
  7. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Just a quick tip, when you're adding an array to a list, you use AddRange, not Add.
     
  8. fr0stiee

    fr0stiee

    Joined:
    May 10, 2013
    Posts:
    3
    Okay, thank you for that, much appreciated.

    Oh okay, thanks for the tip. I'll keep that in mind :)