I been searching this for a while and I am not moving forward at all. Any comment will help. So it you guys have any ideas just comment it.
I tried to do one with playerPref, (Below is the code), BUT It is not working.
public List<string> Levelnames= new List<string>();
void Start()
{
for (int i = 0; i < Levelnames.Count; i++)
{
if (PlayerPrefs.HasKey("levels" + i))
{
PlayerPrefs.GetString("levels" + i, Levelnames*);*
print(“load”);
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
Levelnames.Add(“levels”);
for (int i = 0; i < Levelnames.Count; i++)
{
if (!PlayerPrefs.HasKey(“levels” + i))
{
PlayerPrefs.SetString(“levels” + i, Levelnames*);*
print(“save”);
}
}
}
}
Perhaps something like this?
This add “levels” string to the Levelname list, and then save it to PlayerPrefs when you hit Q.
When you start, it will load the previously saved levels.
public List<string> Levelnames = new List<string>();
void Start()
{
for (int i = 0; PlayerPrefs.HasKey("levels" + i); i++)
{
Levelnames.Add(PlayerPrefs.GetString("levels" + i));
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
Levelnames.Add("levels");
for (int i = 0; i < Levelnames.Count; i++)
{
PlayerPrefs.SetString("levels" + i, Levelnames*);*
}
}
}
Since you didn’t add any string to your Levelnames, Levelnames.Count is always zero.
So you will never enter for loop. What you want to do is setup the list properly on Start:
public List<string> Levelnames = new List<string>();
private const int _numOfLevels = 10;
private const string DEFAULT_NAME = "Unknown";
void Start()
{
//Init Levelnames
for(int i = 0; i < _numOfLevels; i++)
{
Levelnames.Add(PlayerPrefs.GetString("levels" + i, DEFAULT_NAME));
}
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Q))
{
for(int i = 0; i < Levelnames.Count; i++)
{
PlayerPrefs.SetString("levels" + i, Levelnames*);*