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 /
  • Help Room /
avatar image
0
Question by g_v_d · Feb 10, 2018 at 04:18 PM · scenegameover

count how many times a scene is loaded

i have a player, when it dies the scene is reloaded. i want to limit the reload to 3 times and then show game over scene how should i code this?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Nomenokes · Feb 10, 2018 at 05:47 PM

Use a static variable, one that can't be instantiated or destroyed even when changing scenes.

 public static int reloadedNum=0;
 
 //later on
 
 if(reloadedNum<3){ 
      reload();
      reloadedNum++;
 }
 else{
      gameOver();
 }
Comment
Add comment · Show 5 · 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 g_v_d · Feb 10, 2018 at 06:48 PM 0
Share

@Nomenokes I'm a complete beginner I don't have much coding experience I put a void oncollision2d(collision2d col) to load the current scene again ,so how should I apply this to what you wrote.

avatar image Nomenokes g_v_d · Feb 10, 2018 at 07:14 PM 0
Share

Ok I see. Do you have a big script for your player? If so, put the static variable there and do the "later on" in the OnCollisionEnter or whatever you have as a trigger device

avatar image g_v_d · Feb 11, 2018 at 02:17 AM 0
Share

@Nomenokes I'm confused This script is attached to my floor when the player touches the ground the scene is reloaded

public class dying : $$anonymous$$onoBehaviour { [SerializeField]Transform spawnpoint;

void OnCollisionEnter2D(Collision2D col) { if (col.transform.CompareTag ("Player")){ col.transform.position = spawnpoint.position; Scene$$anonymous$$anager.Loadscene(Scene$$anonymous$$anager.GetActiveScene().name); } } } So how should I make the appropriate changes to this to stop reloading more than 3 times?really appreciate your help.thank you very much

avatar image Nomenokes g_v_d · Feb 11, 2018 at 03:55 AM 0
Share
 public class dying : $$anonymous$$onoBehaviour { 
 
 static int timesReloaded=0;
 [SerializeField]Transform spawnpoint;
 
 void OnCollisionEnter2D(Collision2D col) { 
     if (col.transform.CompareTag ("Player"){
         if(timesReloaded<3){ 
             col.transform.position = spawnpoint.position; 
             Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().name); 
             timesReloaded++;
         } else {
             Scene$$anonymous$$anager.LoadScene("gameOverScene);
         }
     } 
 } 
 }


Do you see how that would work?

avatar image Nomenokes Nomenokes · Feb 11, 2018 at 02:56 PM 0
Share

++ means "add one to" by the way

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

88 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 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

Why the StartCoroutine(GameOver()); don't work 0 Answers

Temple Run and Subway Surfers Game Over Implementation? 0 Answers

Why the StartCoroutine(GameOver()); don't work 3 Answers

Gear VR template scene for Unity 5.1+ ? 0 Answers

How to load a scene from AssetBundles from the web using 5.2 AssetBundle system showing download progress? 0 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