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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Making List of GameObjects within the List[Fixed]

Discussion in 'Scripting' started by Piote20, Mar 25, 2018.

  1. Piote20

    Piote20

    Joined:
    Mar 23, 2018
    Posts:
    1
    I am trying to make a List of levels and in the levels make a list of Gameobjects for example obstacles and background animation.I don't want to make a new scene for every new level, I want to have one scene in which i can play more then one level .So far i was using
    public GameObject[] Obstacles;
    and Spawning them with
    Instantiate(Obstacles[UnityEngine.Random.Range(0, Obstacles.Length - 1)]

    FIXED:I fixed it.
    i just had to use the tables and define new class,in this way i can access walls inside levels
    [System.Serializable]
    public class Obstacles
    {
    public GameObject[] Walls;
    }
    public Obstacles[] Levels;
     
    Last edited: Mar 26, 2018
  2. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    818
    Hey Piote, you really need to provide more information if you want someone to help you.You havent really told us what your problem is, what goes wrong and where. What is wrong with what you are using, what error do you see? What do you expect to happen, what is happening instead?

    `I want to have one scene in which i can play more then one level`
    Why do you want this? Separating levels is exactly why Scenes exist, there usually is no need to make your own life harder by doing it another way.
     
  3. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ListInList : MonoBehaviour
    5. {
    6.  
    7.     public List<List<GameObject>> masterList;
    8.     public List<GameObject> obstacles;
    9.     public List<GameObject> otherStuff1;
    10.     public List<GameObject> otherStuff2;
    11.     public List<GameObject> level1;
    12.     public List<GameObject> level2;
    13.     public List<GameObject> level3;
    14.     public List<GameObject> level4;
    15.    
    16.    
    17.  
    18.     void Start() {
    19.         masterList.Add(obstacles);
    20.         masterList.Add(otherStuff1);
    21.         masterList.Add(otherStuff2);
    22.         masterList.Add(level1);
    23.         masterList.Add(level2);
    24.         masterList.Add(level3);
    25.         masterList.Add(level4);
    26.        
    27.         //know you have a list of lists  with GameObjects in each list
    28.        
    29.         //access them like this
    30.         for(int i = 0; i < masterList.count; i++)
    31.         {
    32.             for(int k = 0; k < masterList[i].count; k++)
    33.             {
    34.                 Debug.Log(masterList[i][k].name);
    35.             }
    36.         }
    37.     }
    38. }
     
  4. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    818
    This is not what you want, at all. It is super inefficient and create a ton of garbage. Just use a scene.
     
  5. CDMcGwire

    CDMcGwire

    Joined:
    Aug 30, 2014
    Posts:
    133
    To provide a little more context, based on what your lists seem to be holding, it looks like you're trying to do what Unity already does. There are already the Start/Update/etc methods on every component. Whatever this script is intended to do, consider pushing the logic to those methods on components of the objects themselves rather than a single control script.
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,745
    The root of the problem is this. It's probably best to examine why you think you don't want a new scene for every level. Whatever your reasoning is, I'm pretty confident it's misguided.
     
  7. CDMcGwire

    CDMcGwire

    Joined:
    Aug 30, 2014
    Posts:
    133
    Not necessarily. If each "level" is mostly just a difference in game logic and requires little to no change in assets, then loading a level would be a waste of time and setup. Especially if each level shares assets or should be dynamically generated.
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,745
    Even in that scenario, you can easily ensure it doesn't re-load assets by having one always-on scene whose job is to maintain references to prefabs & assets needed. That way they'd never be unloaded, and you can continue to use Unity's scene creation tools (while also getting benefits of asynchronous loading, etc, which spawning prefabs manually can't give you.)

    But we'll never know because OP edited his post to say he found a solution without replying.
     
  9. CDMcGwire

    CDMcGwire

    Joined:
    Aug 30, 2014
    Posts:
    133
    So it seems. XD
     
  10. Pagefile

    Pagefile

    Joined:
    Feb 10, 2013
    Posts:
    51
    If you didn't want to reload assets, couldn't you use LoadSceneAdditive() before unloading the current scene? I would assume Unity would be smart enough to know which assets are already loaded and which aren't.

    Of course, that's all it is. An assumption. Does anyone know if that's the case?
     
    StarManta likes this.