• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Temseii · Sep 12, 2016 at 06:43 PM · playerprefsloadingsaving

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

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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.

Comment
nj4242
Temseii

People who like this

2 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Temseii · Sep 14, 2016 at 11:46 AM 0
Share

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/M9PP68u4

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?

avatar image

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

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

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 ;)

Comment

People who like this

0 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Temseii · Sep 12, 2016 at 07:27 PM 0
Share

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?

avatar image daleran · Sep 12, 2016 at 07:53 PM 0
Share

@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.

avatar image Temseii daleran · Sep 12, 2016 at 07:59 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

59 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Saving & Loading the scene (or at least one array) via Javascript 1 Answer

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 with PlayerPrefs? How it works? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges