I have a number of scenes.
Each scene has a gem.
I want my game to work like this:
if the player collect the gem, then the game should remember that the it has been collected and which it is (for expample Gem no.3 collected)
Thank in advance.
I would save all the collected things in a class for each level.
[Serializable]
public class LevelData
{
public List<string> CollectedObjects = new List<string>();
}
then you need an object which you can access globally and which helps you to access a certain level data:
public class LevelManager : MonoBehaviour
{
public static LevelManager Instance; // singleton
Dictionary<string, LevelData> levelData = new Dictionary<string, LevelData>();
void Awake()
{
Instance = this;
GameObject.DontDestroyOnLoad(this);
}
public void NotifyItemCollected(string itemName)
{
if(!levelData.ContainsKey(Application.loadedLevelName))
levelData.Add(Application.loadedLevelName, new LevelData());
levelData[Application.loadedLevelName].CollectedObjects.Add(itemName);
}
}
and then you need a script which you can append to your collectables (probably you already have one). Call Collect() when you collect it:
public class Collectable : MonoBehaviour
{
public void Collect()
{
LevelManager.Instance.NotifyItemCollected(this.name);
}
}
please note: all code untested and could be improved to make more error checks and stuff.
You need a GameObject with LevelManager attached (one in the very first level… it will stay for all levels)
all the collectables in a level need unique names.
There might be better ways to do it, but this is a simple approach which works if you don’t make any naming mistakes.