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 everett24 · Jul 04, 2012 at 05:18 AM · playerdeath

How do i make my player die?

Okay so i have hunger and thirst that deplete my health when they reach 0 but my player doesn't die when his health reaches 0, how do i do this? my health script is in C#(i think i made it look like code):

 using UnityEngine;

using System.Collections;

public class PlayerHealth : MonoBehaviour { public float maxHealth = 100f; public float curHealth = 100f;

public float healthBarLength; private Thirst thirst; private Hunger hunger;

// Use this for initialization void Start () { healthBarLength = Screen.width / 2; thirst = GetComponent(typeof(Thirst)) as Thirst; hunger = GetComponent(typeof(Hunger)) as Hunger; } // Update is called once per frame void Update () { AddjustCurrentHealth(0); if (thirst.thirstLevel <= 0 || hunger.hungerLevel <= 0) { //variable declaring delay of however many seconds //int delay = 10; //delay-= 1; //if (delay == 0){ //if(delay = 0) execute following line curHealth -= 0.01f; //}

} }

void OnGUI() { GUI.Box(new Rect (10, 10, healthBarLength, 20), curHealth + "/" + maxHealth); } public void AddjustCurrentHealth(int adj){ curHealth += adj;

if(curHealth < 0) curHealth = 0;

if(curHealth > maxHealth) curHealth = maxHealth;

if(maxHealth < 1) maxHealth = 1;

healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth); } }

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
2
Best Answer

Answer by fafase · Jul 04, 2012 at 08:06 AM

Personal opinion, I would not destroy the main character, just a point of view, I would rather respawn it. Main characters generally have a lot of information and many things happens in the Start(), particularly expensive Find() functions. Destroying and instantiating a new character might cause some lagging. If you simply reposition him somewhere with reinitialization of the variables, it won't show. On top of that, other scripts like enemies are probably using the character for target or other purpose, you could get a Null reference if you destroy the object.

 function Update(){
     if(health<=0)
         Die();
 }
 
 function Die(){
      transform.position = Vector3(x,y,z);
      health = 100;
      // other codes
 }
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 everett24 · Jul 04, 2012 at 03:23 PM 0
Share

$$anonymous$$y health script is in c# so when I try to call my health variable it throws a fit. Would it be the same thing in c# as it is in javascript? I'm not the best at translating code between languages.

avatar image
0

Answer by BeHappy · Jul 04, 2012 at 07:42 AM

Have a check for,

 if(health == 0)
 {
    Destroy(gameobject);
 }

That will destroy the player.

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 AlucardJay · Jul 04, 2012 at 07:45 AM 0
Share

as health is a float, I would use <= 0 e.g.

 if(health <= 0)
 {
    Destroy(gameobject);
 }

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

9 People are following this question.

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

Related Questions

I want my player to die when collide with enemy from front or back and not when it jumps on it (i alread tried checking if player is grounded or not) and if someone could provide script for it ill be glad 0 Answers

How do I get rounds to work on a wave-based game? 1 Answer

Player Will not take any damage! 1 Answer

I want to create a radiation scall in the top corner that continues going up until you get to the end of the map. I then want the player to die if the scale reaches the end and they havent reached the end of the map 0 Answers

[UNITY 2D] Can I create a spawner that spawns another enemy after its death using IF statements? 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