Hi
Question from a newbie, so sorry for the low level… 
Is it possible to use a serialized variable to replace (depending on the scene) the name of a list to be called?
- Scene1: call List1
- Scene2: call List2
- etc.
The script doesn’t change between scenes.
I couldn’t do it with the principle below:
// I have several lists and want to use one or another, depending on the scene.
[SerializeField] private List<string> List1 = new List<string>();
[SerializeField] private List<string> List2 = new List<string>();
[SerializeField] private List<string> List3 = new List<string>();
// I have a string for the name of the list (depending on the scene)
[SerializeField] private string selectedList;
// And this doesn't work
myResult = selectedList[5];
Thanks for your help!
I guess an important lesson with programming is when you feel the need to “find a variable by name”, which is what’s known as reflection, you usually want to rethink your approach unless you absolutely know what you’re doing.
Though I guess more info is needed here. When you say “the script doesn’t change between scenes”, do you mean this is on a global (perhaps DontDestroyOnLoad gameobject) that is persistent? Or do you mean you reuse the same component across multiple scenes?
Because if it’s the latter, you only need the one list, and just assign different values as needed to the different component instances in each different scene.
If the former, you want to create an object that represents information about each level. This is where scriptable objects are probably the best choice: Unity - Manual: ScriptableObject
1 Like
You can also play around with an array of List or List of List instead of having 3 separate Lists:
[SerializeField] private List<List<string>> allLists...
Then your field “selectedList” could be an int instead of string and on level 0 you set it to 0, on level 1 to 1 etc.
Then you get your list by using: allLists[selectedList].
It is maybe not the best thing to do in case you change the order of your scenes around but can’t hurt to try it out and learn something new for later 
FYI Unity can’t serialize lists of lists, jagged arrays, 2d arrays, or anything beyond List<T>
or an array.
It would need to be a collection of a serializable plain C# object, which itself contains a list.
1 Like
Thank you so much for your answers! There’s a lot of valuable information I need to digest…
I’m pretty sure that my way of programming is still far from being standard and rigorous!..
To be more informative and concrete, I have the same script on 4 scenes. Each scene has a horizontal selector (a sort of drop-down list), and each selector is linked to the one in the previous scene:
- First scene, first selector with Item1, Item2, Item3, …
- Second scene, second selector which I have to fill with the ‘right’ list (for example, if the user has selected item 2 in the first scene, the selector will be filled with list ‘2’ with item 2-1, item 2-2, item 2-3. But if he has selected item 3, the selector will be filled with list ‘3’ with item 3-1, item 3-2 and item 3-3).
That’s why I thought it should be possible to select lists by name.
Without knowing what data you want to have in each list, I’d go with my previous suggestion of using scriptable objects.
Encapsulate the data in a scriptable object class, and make and set up the instances as needed. Then in this component you have in each scene, serialise a list of this scriptable object type. Then you can just references the one you want to have available in each scene on a per-component basis.
My sloptastic fix is making lots of 1D lists for the Inspector, then assembling the 2D list later, by hand. Something like this:
public Cows[] Lvl1Cows, Lvl2Cow, Lvl3Cows, Lvl4Cows;
public Cows[][] AllCows;
void Start() {
AllCows = new Cows[4][]; // or is this backwards?
AllCows[0]=Lvl1Cows;
AllCows[1]=Lvl2Cows;
...
}
It’s quick and just as easy to enter Inspector data this way than with a more clever solution (using a dummy class to get a “real” Inspector-based array of arrays with fold-outs inside of foldouts).