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 FEZ17 · Mar 14, 2020 at 03:29 PM · coin

how keep an object destroyed when scene restarts due to player death?

I am new to coding and I would like to know how to keep my object (coin) destroyed when the player collects it than dies and respawns which resets the scene and the coin comes back so the player can keep tallying his score up by dying. here is my code for the coin

{

 public AudioSource collectSound;
 
 void OnTriggerEnter(Collider other)
 {
     collectSound.Play();
     ScoringSystem.theScore += 10;
    
    Destroy(gameObject);
 }
 

and here is my code that makes the player respawn

 void OnCollisionEnter(Collision collisionInfo)
 {
     if (collisionInfo.collider.tag == "obstacle")
     {
         movement.enabled = false;
         FindObjectOfType<GameManager>().EndGame();
     }

 }

here is my code for when the game restarts in my Gamemanager script

    public void EndGame()
 {
     if (gameHasEnded == false)
     {
         gameHasEnded = true;
         Debug.Log("Game Over!");
         Invoke("Restart", restartDelay);
     }
 }
     
  public void Restart()
 {
     SceneManager.LoadScene(SceneManager.GetActiveScene().name);
 }

   
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by BBIT-SOLUTIONS · Mar 14, 2020 at 03:43 PM

There are different approaches:

It seems like the coin is inside your scene. So you could create e.g. a "GameManager"-singleton which keeps existing, when you swich between scenes ( DontDestroyOnLoad() ) . Or you can just simply use a static class (no Monobehaviour) for the same purpose. Then make e.g. a counter or a boolean to check, if you open the scene a second time (after the die) and then destroy the already collected coins on Awake(). For this approach, each Coin should probably have an id and your "GameManager" needs a List or Array, so that you know, which ones you have to destroy (when you not just want to destroy either none or all).

Alternatively you could take a similar approach, but don't place the coins hard in the scene. You could just spawn the ones by script dynamically on runtime at scene start, which you really need. The other logic is similar to the first approach.

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

Answer by 3kWikiGames · Mar 14, 2020 at 08:03 PM

Another easier way you could do this, is to only make it appear that the player has died, but dont load a new scene, and just reset the position of your player to a spawn point.

Comment
Add comment · 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 BBIT-SOLUTIONS · Mar 14, 2020 at 09:42 PM 0
Share

Yes, if possible, i would also suggest this approach.

Question was just specifically about approaches, which you can use on a scene restart. But when that is not necessary (because of possible other reasons), then, i agree, you shouldn't reload the whole scene and just respawn the player ins$$anonymous$$d.

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

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

Physics Material slow down the game. 2 Answers

Admob rewarded video give more and more reward. Why 3 Answers

Spawn object every 5 coins collected 2 Answers

How can I save the gold in my game scene to the main menu scene? 1 Answer

How to create a score system 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