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 san0009 · May 18, 2018 at 04:57 AM · unity 5unity5health-deductionhealth

what is wrong with this???? game is not restarting when hp is zero

using UnityEngine;

public class PlayerCollision : MonoBehaviour { public GameManager gameManager; public float health = 100;

 void Start ()
 {
      
 }

 void OnCollisionEnter (UnityEngine. Collision collisionInfo)
 { 
     if (collisionInfo.gameObject.tag == "Obstacle") 
     {
         float health = 100; 
         Debug.Log ("-100"); 
     }
 } 
 public void TakeDamage (float amount)
 {
     health -= amount; 
     if (health <= 0) 
     {
         FindObjectOfType <GameManager> ().EndGame (); 
     }
         }

}

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 san0009 · May 18, 2018 at 04:59 AM 0
Share

Game $$anonymous$$anager

using UnityEngine; using UnityEngine.Scene$$anonymous$$anagement; public class Game$$anonymous$$anager : $$anonymous$$onoBehaviour { bool gameHasEnded = false;

 public float restartDelay = 1f; 

 public GameObject completeLevelUI; 

 public void CompleteLevel()

 {
     completeLevelUI.SetActive (true); 
 }

 public void EndGame ()
 {
     if (gameHasEnded == false) 
     {
         gameHasEnded = true; 
         Debug.Log ("GA$$anonymous$$E OVER"); 
         Invoke ("Restart", restartDelay);  
     }
      
 }
 void Restart ()
 {
     Scene$$anonymous$$anager.LoadScene (Scene$$anonymous$$anager.GetActiveScene ().name);
 }

}

avatar image san0009 · May 21, 2018 at 12:39 AM 0
Share

using UnityEngine;

public class PlayerCollision : $$anonymous$$onoBehaviour { public Game$$anonymous$$anager game$$anonymous$$anager; public const float maxHealth = 100; public float currentHealth = maxHealth; void Start () {

 }

 void OnCollisionEnter (UnityEngine. Collision collisionInfo)
 { 
     if (collisionInfo.gameObject.tag == "Obstacle") 
     {
          float maxHealth = 100; 
         Debug.Log ("-100"); 
     }
 } 
 public void TakeDamage (float amount)
 {
     currentHealth -= amount;
     if (currentHealth <= 0)
     {
         currentHealth = 0;
         FindObjectOfType <Game$$anonymous$$anager> ().EndGame (); 
     }
         }

}

1 Reply

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

Answer by Vicarian · May 18, 2018 at 05:08 AM

You have nothing calling TakeDamage in the scripts you've posted.

Comment
Add comment · 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 san0009 · May 21, 2018 at 12:38 AM 0
Share

How to call the TakeDamage

avatar image Vicarian san0009 · May 21, 2018 at 12:54 AM 0
Share

In your OnCollisionEnter event, where you check to see if the colliding object is "Obstacle", call TakeDamage(maxHealth); though your design probably isn't correct, so the collision event might not fire in the first place.

avatar image san0009 Vicarian · May 21, 2018 at 11:49 PM 0
Share

Thank you it works

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

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

How to make a good hp system in unity 0 Answers

How to render UI Text in front of 3d gameObject on ScreenSpace Camera 0 Answers

Error cs0103: the name unityEngine don't exist in the current context 2 Answers

Unity 5 - How to duplicate enemies whilst making them unique? 1 Answer

Error initializing license system Please contact suppport @unity3d.com,Ошибка про запуске юнити 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