- Home /
Working with Lists in Unity3d
I am looking for help working with List<> and I am hoping that somebody can give a short explanation of all the points that I am asking.
a) In the tutorials online, I keep seeing how to initialize a new List, such as:
List< int > Level = new List< int >();
However, I don't see an explanation for a List<> declaration (assuming I created a List<> on my character script beforehand and want to continue using data I saved earlier). It appears to me that initializing a new List would effective create something new, not continue the data I saved earlier. How would I bring back my data from a previous instance of my game inside a List variable???
b) During game testing with Scene and Console how do I used Debug.Log to show all the saved data I have in my List variable??? Or simply, how can I view all the data inside my List in the editor if I make it a public variable??
c) Let's say I want to create a List that holds 2 pieces of information, like
(Player Level) and (ExperiencePointsToNextLevel)...
Do I have to make both of them int variables or can I have one float and one int variable??? How would I declare something like that??
It doesn't sound like you understand what a List is. A List is a type defined by C# and it can only hold one type of data, so you have to pick if you want it to hold ints or floats. To do something like you're talking about with PlayerLevel and ExperiencePointsToNextLevel you probably want to use a struct or a class.
your comment is slightly contradictory - there's no picking ints or floats - the List
can be some struct/class...as you state ;)
a) How would I bring back my data from a previous instance of my game inside a List variable???
You cant, you would hav to loop through the list and save it in playerprefs for example, then read them back into the list.
b)During game testing with Scene and Console how do I used Debug.Log to show all the saved data I have in my List variable??? Or simply, how can I view all the data inside my List in the editor if I make it a public variable??
Assu$$anonymous$$g it is a public list, while in-game, go into scene view, click on the object and look in the inspector, the list will be there.
c)Do I have to make both of them int variables or can I have one float and one int variable??? How would I declare something like that??
you could do something like this :
[System.Serializable]
public class PlayerData
{
public int Level = 0;
public float PointToNExtLevel = 0;
}
public class Player : $$anonymous$$onobehaviour
{
public List<PlayerData> Data;
}
Answer by SunnyChow · Jan 08, 2016 at 01:54 AM
a)
I am not sure what you mean
If you want all objects share one list, you can make it static.
static List< int > Level = new List< int >();
b) you need a for-loop
string result = "";
for(int i = 0 ; i< Level.Count;i++){
result +=Level[i]+",";
}
Debug.Log(result);
c) I suggest you not to use List
int level;
float exp4nextLevel;
or you can use Hashtable, but you will have to remember the type and purpose of each value by yourself, which is not very convenient.
Your answer
![](https://koobas.hobune.stream/wayback/20220612055404im_/https://answers.unity.com/themes/thub/images/avi.jpg)