- Home /
How to assign variables to a scene (Solved)
In my game I have a bunch of different levels. In every level you can receive a gem. You can't earn more than 3 gems per lvl. But I don't know how I can implement my system. I have a globalUpgradesManager that never gets destroyed and receives the gems from the levelManager. (eacher level has it's own levelManager)
So when you clear the level the levelManager calls a function in the globalUpgradesManager. This function adds gems based on the health that you finished with on the level. The receiving the gems works. But you can get gems every time you play the level. So instead of getting a max of 3 gems per level you can get as many as you want.
How can I solve this problem? Thank you in advance
Code: public void ReceiveOneGem() { Scene currentScene = SceneManager.GetActiveScene(); string sceneName = currentScene.name;
if (!levels.Contains(sceneName))
{
levels.Add(sceneName);
//gemsPerLevel.Add(maxGemsPerLevel);
//maxGemsPerLevel = 0;
gems++;
maxGemsPerLevel += 1;
sceneName = maxGemsPerLevel.ToString();
}
else if (levels.Contains(sceneName))
{
if (maxGemsPerLevel <= 3)
{
gems++;
maxGemsPerLevel += 1;
sceneName = maxGemsPerLevel.ToString();
}
}
}
public void ReceiveTwoGem()
{
Scene currentScene = SceneManager.GetActiveScene();
string sceneName = currentScene.name;
if (!levels.Contains(sceneName))
{
levels.Add(sceneName);
//gemsPerLevel.Add(maxGemsPerLevel);
//maxGemsPerLevel = 0;
gems += 2;
maxGemsPerLevel += 2;
sceneName = maxGemsPerLevel.ToString();
}
else if (levels.Contains(sceneName))
{
if (maxGemsPerLevel <= 3)
{
gems += 2;
maxGemsPerLevel += 2;
sceneName = maxGemsPerLevel.ToString();
}
}
}
public void ReceiveThreeGem()
{
Scene currentScene = SceneManager.GetActiveScene();
string sceneName = currentScene.name;
if (!levels.Contains(sceneName))
{
levels.Add(sceneName);
//gemsPerLevel.Add(maxGemsPerLevel);
//maxGemsPerLevel = 0;
gems += 3;
maxGemsPerLevel += 3;
sceneName = maxGemsPerLevel.ToString();
}
else if (levels.Contains(sceneName))
{
if (maxGemsPerLevel <= 3)
{
gems += 3;
maxGemsPerLevel += 3;
sceneName = maxGemsPerLevel.ToString();
}
}
}
A simple way would be to store the currently collected gems per level in a file. Have a look at this website, it explains it quite well. You can then check the leftover gems per level by simply reading from it before you load the level scene.
Answer by xxmariofer · Jan 14, 2019 at 05:08 PM
Hello, just change your list of names with a dictionary that contains the number of gems you got in that level using the scene name as key. Cant provide know code i will edit this answer in a few minutes.
Thank you for the respone, I think you showed me the right direction. If you could provide some sample code that would be great
Thanks a lot
Thank you xxmariofer, because of you I found the answer to my problem.
You have my gratitude.
Hello, glad hearing that, you need any extra help or managed to get it working?
It is completely working now thanks to you
Your answer
Follow this Question
Related Questions
Get data from Server 0 Answers
Getting an "Enum" value from a Scriptable Object 2 Answers
One Script Referred by Another Script used by Multiple Objects 0 Answers
How to get data from Scriptable Objects without copy-pasting code 1 Answer
Instantiate ScriptableObject with a random asset from List 1 Answer