How would you go about adding a container to a list? Each container will have a list inside.
For example I have a star system #1 with list of all the planets, then star sytem #2 with a list of all the planets etc, then planets with a list of all their satellites.
List StarSystem = new List(StarSystem1, StarSystem2, StarSystem3); // Star Systems List
StarSystem1 would have a planet list connected to it, so will the other systems
List StarSystem1 = new List(Planet1, Planet2, Planet3, Planet4); // planets list
Planet1 will have a list of all the satellites and so will the others.
List Planet1 = new List(Sat1, sat2, sat3); // Satellite list
First i need to know what this is called in the area where it says myownclass, its not a string, not an integer.
Who knows, maybe the coding is correct.
I also need an example what the best way is to retrieve and insert the data.
A generic list contains items of a specific type e.g. Planet, StarSystem, Sat in your case. The type declaration for the list would be then:
List<StarSystem> starSystems = new List<StarSystem>();
List<Planet> planets = new List<Planet>();
List<Sat> satellites = new List<Sat>();
If you want to place one list in another it would look like:
List<List<List<Sat>>> starSystems = new List<List<List<Sat>>>();
starSystems[0] //is a list containing a list with satellites (a starSystem)
starSystems[0][0] //contains a list of satellites (a planet)
starSystems[0][0][0] //contains a satellite
This is really ugly so I would recommend to create some Objects and encapsulate them e.g.
StarSystems[0].Planets[0].Satellites[0]...
To put a list inside i list you do this:
List<List<Class>> StarSystem = new List<List<Class>>(StarSystem1, StarSystem2, StarSystem3);
Or you can do something like this, sorry did not use correct programming naming conventions but you get the point. hope that helps out
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Test : MonoBehaviour {
[System.Serializable]
public class MySweetData
{
public List<int> IntTest;
}
public List<MySweetData> tested;
// just a little data test trial
void Start ()
{
if (tested.Count == 1)
{
if(tested[0].IntTest.Count ==1)
int d = tested[0].IntTest[0];
else
Debug.Log("IntTest is empty");
}
else
{
Debug.Log("test is empty");
}
}
This is an old post but it’s a common issue. You can have a List>
I had a dictionary originally which worked for a while but wasn’t efficient for dynamically populating drop down menus among other things.
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
protected Scene thisScene;
protected List<GameObject> rootObjects;
public List<GameObject> planetList;
public List<GameObject> orbitList;
public List<GameObject> levelsList = new List<GameObject>();
public List<List<GameObject>> planetsListWithLevels = new List<List<GameObject>>();
//public Dictionary<string, GameObject> levelsDictionary;
public void GetSolarSystem() {
thisScene = SceneManager.GetActiveScene(); // get the scene
rootObjects = new List<GameObject>(); // list or root objects
thisScene.GetRootGameObjects(rootObjects); // get the root objects
orbitList = new List<GameObject>(); // list of my orbits
planetList = new List<GameObject>(); // list of my planets
levelsList = new List<GameObject>(); // list of my levels
planetsListWithLevels = new List<List<GameObject>>(); // list of level lists
//levelsDictionary = new Dictionary<string, GameObject>(); //alternate way of storing via dictionary
foreach (GameObject obj in rootObjects) { // for every root object
if (obj.name.ToLower().Contains("orbitplanet")) { // if it has orbitplanet inname
levelsList = new List<GameObject>(); // list of my levels
orbitList.Add(obj); // add it to my orbits
planetList.Add(obj.transform.GetChild(1).gameObject); // add the the second child to my planets
foreach (Transform level in obj.transform.GetChild(1)) { // go through planet's children
levelsList.Add(level.gameObject); // add the levels to levels list
//levelsDictionary.Add(obj.transform.GetChild(1).name + level.gameObject.name, level.gameObject); // alternately add to <string,GameObject> dictionary
}
planetsListWithLevels.Add(levelsList); // add my list to the the list of lists
}
// this is to verify the list of lists
for (int i=0;i<planetsListWithLevels.Count;i++){ // for each list
for (int j=0;j<planetsListWithLevels*.Count;j++) { // for each item in list*
print("list : “+i +” level name : "+ planetsListWithLevels*[j].name); // prints “list : 0 level name : Level1” … etc*
}
}
}
}
Finally found a way.
Since I had to find a way around activation objects without finding them, I made this code, it will work for anything else I need.
-
Find all my 810 planets at start, which is very fast. I use the same for my 90 stars.
-
Now your able to turn them off and on when ever you like.
The code is easy and short
Here we initialize is:
public GameObject [,] mainPlanetList = new GameObject [10,10,10];
void Start () {
findMainPlanetFolder();
}
void findMainPlanetFolder() {
for (int g = 0; g <=9; g++){ // Galaxy
for (int s = 0; s <=9; s++){ // Star
for (int p = 0; p <=9; p++){ // Planet
mainPlanetList[g,s,p] = GameObject.Find(“Galaxies/” + g + “/Stars/” + s + “/Planets/” + p + “/Main”);
}}}}
void updatePlanetFolder() { // Here I disable and enable the Planet.
for (int p = 1; p <=9; p++){
if (mainPlanetList[activeSystemM[0],activeSystemM[1],p]) { //
if (activeSystemM[2] == p) mainPlanetList[activeSystemM[0],activeSystemM[1],p].SetActive(true); // On
else mainPlanetList[activeSystemM[0],activeSystemM[1],p].SetActive(false); // Off
}}}
activeSystemM is the location I am, 0 for galaxy, 1 for star, 2 for planet, 3 for satellite, and tells which galaxy/Star/Planet I am currently on.