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 :slight_smile:

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.

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 :slight_smile:

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 :wink: