Hi, I would like to create a List where it should be accessible in every scenes. The List remove 1 of its item every scene. How can I retain the updated values of the List in each scenes?
Hi @Playplyfun you could use playerprefs probably a loop however long the list is and everytime an item leaves the list change the value of an integer player pref to maybe 0 or 1 to show change then repopulate the list in next scene with a loop and if statement.
Something like:
string x = “”;
for(i = 0; i < length of list; i++)
{
x = “listname” + i.ToString();
if(PlayerPrefs.GetInt(x,0) == 1)
{
//meaning item was removed at that index
List.remove(i)
}
}
You have three options:
- Make a static class. (Reference: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members)
- Make a MonoBehaviour object that
persists through scene changes (Reference: Unity - Scripting API: Object.DontDestroyOnLoad). - Use a Scriptable Object (Reference: Unity - Manual: ScriptableObject).
Here’s an example using a static class.
public class NumberRemover : MonoBehaviour
{
private void Start()
{
NumberData.numbers.RemoveAt(0);
}
}
public static class NumberData
{
public static List<int> numbers = new List<int>()
{
1, 2, 3, 4, 5, 6, 7, 8, 9
};
}