Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 DanielB89 · Oct 22, 2014 at 08:06 PM · gameoverlivesshieldhealth

How to destroy and respawn the player after loosing a life?

I want to destroy the player object and reset it to the original position it was when the game start. I also want to display a game over screen after the live reaches zero.

 public class ShipPlayerController : MonoBehaviour {

 // wrecked into an enemy?
      if (other.tag == "EnemyShip") {
         ModShield(-25);
          if ( CurShield <= 0) {
             ModHealth(-1);
          }
          if( CurHealth <= 0) {
             ModLives(-1);

          }
 
 }








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
2

Answer by bubzy · Oct 22, 2014 at 08:56 PM

why destroy the player? just move it back to the start position. if you want to make an explosion or something, just move the player offscreen, it will save resources.

 //playerscript
 
 
 public Transform startLocation; //drag a empty gameObject into this on the inspector, then move the empty gameObject to where you want the player to "respawn"
 
 //then
 
 if (CurHealth <=0){ 
 ModLives(-1);
 //do some explody thing
 //if you need to "hide" the player just use something like 
 //transform.position = new Vector3(-999f,-999f,-999f);
 transform.position = startLocation.position; // move the player to the position of the empty gameObject
 }
 

also I see a lot of people naming their variables with a capital letter, while this is absolutely your own choice, its nicer to start with a lowercase letter and capitalise the second word(if there is one) not only does it look nicer, but you can avoid clashes with reserved words(sometimes). just a thought :)

look at GUI options for the game over screen, there are some great tutorials available. the code would be

 if(lives == 0)
 {
 //display game over screen
 }

but I guess you could work that our for yourself :)

all the best with your game

Comment
Add comment · Show 2 · 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 DanielB89 · Oct 22, 2014 at 09:42 PM 0
Share

Thanks. I already have a game over screen the problem is that I need to get the screen to fade to the scene

avatar image MarksTeq · Oct 22, 2014 at 09:53 PM 0
Share

There are a lot of tutorials online about screen fading. See: Screen Fader Tutorial

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Character losing life event not firing 1 Answer

Why OnCollisionEnter doesn't subtract properly player health 1 Answer

How can I make scripts work with other scripts 4 Answers

How to slowly decrease health script? 3 Answers

how to setup health, respawn/checkpoint, lives systems 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