- Home /
How to efficiently hand out rewards based on the level that the player completed?
I currently have 15 levels and counting in my game, and would like to make a simple formula that increases the reward by 10% by every level for example. My "win" screen is a separate scene which is loaded after the level is complete, and that is also where the player will be given his reward based on their performance in the level they just completed.
I'm not sure how to get that information though. How do I let the script know which scene the player just completed?
Any help is very welcome! Thanks in advance :)
Answer by daleran · Sep 12, 2016 at 09:57 PM
Yeah, here is some chicken scratch code for how a simple game stats class or something would work.
public static class GameStats
{
public static int LastLevelCompleted = 0;
public static float BaseReward = 10f; //coins or somthing
public static float RewardModifier = 1.1f //Multiply the current level by 1.1 to get the modifier
public static float GetReward ()
{
return BaseReward * (float)lastLevelCompleted * RewardModifier * BaseReward;
}
}
Now whenever you complete a level you can just set the last level completed by calling:
GameStats.LastLevelCompleted = levelNumber; //or whatever variable you use to store the level number
And when you are on the rewards screen you can call:
float levelReward = GameStats.GetReward();
Now if you want to save that data between playthroughs you can store it in PlayerPrefs by calling:
PlayerPrefs.SetInt("LastLevelCompleted", levelNumber);
Hope this helps.
Thanks a lot for your detailed comment! Sorry I couldn't get to reply earlier.
I'm having a little trouble understanding the levels part. How should I save the levels that I have?
Here's the function I had made for my rewarding the player: http://pastebin.com/$$anonymous$$9PP68u4
It rewards the player like I want it to, but of course like this the reward will be the same regardless of the level completed. How could I apply your method to what I'm already using?
EDIT: I got it working as it should now! Just one more thing: saving levels. Currently I'm just using using an if statement to get and set the value of each level and then calling the function from my start method in the level scenes. Surely there must be a more efficient way?
Answer by iFallOffStuff · Sep 14, 2016 at 03:09 AM
If it's some kind of race or course you could set up triggers at the end of each level, then once that is triggered it switches to the win screen and does something like:
public int var1;
public int currentvar1;
var1 = currentvar1 * 1.10
I'm not too sure if that code's correct, I only started Unity and C# recently.
That's probably not what you're looking for... if it's something different you want let me know, I could try and help :)
Answer by iabulko · Sep 12, 2016 at 07:17 PM
There are a few ways of transferring data through scenes. Some of it may be:
Making a static variable - You can create a static class that is called let's say GameState in which you can store variables like static int lastCompletedLevelNumber or static int lastLevelScore and so on. Then just call GameState.lastCompletedLevelNumber to check what was that. (I recommend this one)
Making an object that has a script with progress data in it to not be destroyed while loading new scene by using GameObject.DontDestroyOnLoad, and call its data from new scene.
Using PlayerPrefs
I'm sure there are more ;)
Hey, thanks for the reply! I'm somewhat familiar with these methods, but I'm not really sure how I could apply one of them to what I'm trying to do here. I understand what you're saying with the static method but I fail to see the full picture of it .
Could I just check which scene is loaded at the start of every level and call that with an if statement from the victory scene?
@Temseii That would work, but would need to be implemented on each scene. You would also run into issues if you ever wanted to implement branching levels.
Static variables are useful because they belong to the class, not the instatiation. So if you had a hundred instances of a level script then the static int LevelsCompleted would be the same for all the instances. Statics members are a great way to share information between a bunch of the same classes. It would be helpful to learn about them.
Thanks for the reply!
I've been using statics a little and have basic understanding of them, but obviously have more to learn. Could you in more detail shed some light on how I should use them in my situation?
Your answer
Follow this Question
Related Questions
Save/Load Animation State of Instantiated Prefabs 0 Answers
Saving and load from player prefs in Unity3D on mobile devices?? 0 Answers
Problems with Playerprefs script 1 Answer
Saving Scenes and loading GameObjects? 4 Answers
Saving & Loading the scene (or at least one array) via Javascript 1 Answer