List name from String

Hi, :wink:
I need to copy List in different script to this one and then work with new list (to avoid changes in original list). Problem is that i have more Lists for each level so I would like to read only List for currently selected Level, however I cannot get copy of List by variable string… ListPlayer=new List(Name); :frowning:
Question is why Name as string doesnt work and how to solve this.

//---Load level selected
string Levels=InternalMemory.LevelSelection;//---Return "Tutorial"

//---List accoring selected level:
string Name="InternalMemory.ListPlayer_"+Levels;
//---Return "InternalMemory.ListPlayer_Tutorial"
ListPlayer=new List<string>(Name);
//---ERROR cannot convert from 'string' to 'System.Collections.Generic.IEnumerable<string>

//---Original (Works):
ListPlayer=new List<string>(InternalMemory.ListPlayer_Tutorial);

Thanks :slight_smile:

First. Read more about lists, constructors and how they work.

But I’m gonna help you anyway:

In your working example you create a new list with strings copying the content of another List of strings. You are calling a constructor when you say new List and there is a special constructor which let you pass another list as a source.

So the ListPlayer_Tutor is not string, it’s a proper List as well.
Instead you are passing strings which is nothing a constructor of list can work with. Create a new list instead and pass it to your other list as you do in your working example.

1 Like

Thank you for answer. I have already used list to list, it works. But my concern is that i cannot simpy unite two strings and use it as name of variable / void etc. :frowning:

If you want a one-element list of strings containing Name, this is the syntax:

The variable:

private List<string> ListPlayer;

Creating the list out of one entry, the string variable Name:

ListPlayer = new List<string>() {
   Name,
};

I don’t know that this is possible (maybe in any language). The only thing I can think of would be to store a master list with all the possible names as strings, then draw from that, because you can access them by name.

1 Like