Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by aan_comel · Jan 15, 2016 at 09:19 AM · c#arrayloop

set level to be played only 3 times

How to set level to be played only 3 times. Player will only get to Play the level 3 times. Can anybody give some examples of code in C#

Comment
Add comment · Show 2
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 hexagonius · Jan 15, 2016 at 12:03 PM 0
Share

3 times each time he runs the game? 3 times in total? Is it the very first or is it manually loaded?

avatar image aan_comel hexagonius · Jan 18, 2016 at 03:25 AM 0
Share

yes, 3 times every times he run the games.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by shriya · Jan 15, 2016 at 01:27 PM

Hi aan_comel,

You just need static counter. In previous answer mistake is at line where we are setting player prefs on button click using counter it should be through fetching playerprefs but as you don't need it so not correcting that.

 public static int counter = 0;
 
 void LoadLevel1()
 {
 if( counter<=3)
 {
    counter++;
   
     Application.LoadLevel("levelName");
 
 }
 else
 {
  print("level already loaded for three times");
 }
 }






Comment
Add comment · Show 8 · 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 aan_comel · Jan 18, 2016 at 03:22 AM 0
Share

Thank you for the codes. I set LoadLevel1() to public and attached it to the button that loads the level. But the counter doesn't seem to count. Even I press the button more than 3 times, it doesn't print "level already loaded for three times"

****UPDATE This code works now! But I didn't realize I will not be able to play again, after I exit the game. Do I need to add script to restart again?

avatar image SterlingSoftworks aan_comel · Jan 18, 2016 at 08:04 AM 0
Share

Reply to "Update"

You can either have when you exit the game, just before the exit code, you set the counter to 0 and save it.. Or in an Awake() function set the counter to 0 and then save it. :)

Edit: And if you're needing the counter to be reset every time they play, there's no need for "PlayerPrefs" and what not.. You should just be able to get away with making the counter static

avatar image aan_comel SterlingSoftworks · Jan 18, 2016 at 08:15 AM 0
Share

Can I just use this code? Or is it not encouraged to do so?

PlayerPrefs.DeleteAll ();

Show more comments
Show more comments
avatar image SterlingSoftworks · Jan 18, 2016 at 07:19 AM 0
Share

The problem with this code up here might be that you need to add in a PlayerPrefs.Save somewhere. This way the PlayerPref is 100% saved.

Another way to do this would be to make the "counter" static. This way, even when you load between scenes and what not, the number always stays.

avatar image shriya · Jan 18, 2016 at 09:27 AM 0
Share

I have edited the code without player prefs .you can check that.

avatar image aan_comel shriya · Jan 18, 2016 at 09:31 AM 0
Share

O$$anonymous$$G. Thank you soo much! You guys help me alot!! Thanks again Big smiles

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

I dont know why im getting a null reference exception 1 Answer

What's the best way to find the smallest Vector3.Distance of an array of enemies? 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to deactivate all GameObject in a array, except last one 4 Answers


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