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 MrRandomFella · Jun 10, 2014 at 06:38 AM · damagelifehealth

Player Health Not Working?

Hi, I have a player with three lives (The lives are represented by gameobjects). My script is meant to make the player lose a life when they collide with a hazard (Trigger), they can hit only three hazards before being destroyed. The problem is, that when I hit a hazard; my player loses one life, and on the second hit, they lose both leftover lives and the player is destroyed instantly. Why does this happen all of a sudden? I had my script working perfectly beforehand. :/

Script:

 #pragma strict
 
 // Controls player health using 3 lives, 2 of which are visable
 
 var fullHealth : int = 3;
 var curHealth : int = 3;
 var player : GameObject;
 var explosion : GameObject;
 var life1 : GameObject;
 var life2 : GameObject;
  
 function OnTriggerEnter(collider : Collider) {
     if(collider.tag == "Hazard") {
         curHealth -= 1;
     }
 }
 function Update () {
      if(curHealth >= fullHealth){
     curHealth = fullHealth;
 }
     if(curHealth == 2){
         curHealth = 2;
         Destroy(life2);
         }
     if(curHealth == 1){
         curHealth = 1;
         Destroy(life1);
         }
      if(curHealth <= 0){
         curHealth = 0;
         Destroy(player);
             Instantiate(explosion,transform.position, transform.rotation);
         }
 }
 
Comment
Add comment · Show 3
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 TobiUchiha · Jun 10, 2014 at 06:47 AM 0
Share

Nothing wrong with the script.

avatar image MrRandomFella · Jun 10, 2014 at 06:50 AM 0
Share

That's what I thought, but it doesn't seem to work? - It seems that whenever the player hits a hazard, the lives randomly subtract. *hits a hazard, loses two lives. Second game hits a hazard, loses all three.

avatar image smirlianos · Jun 10, 2014 at 07:08 AM 0
Share

Check if you have the trigger object duplicated by accident, so the player enters 2 colliders at the same time.

4 Replies

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

Answer by MrRandomFella · Jun 10, 2014 at 10:31 AM

Alrighty! I've just come across something very strange, my player was dying on the second hit with the "average sized" trigger. When I shrank it down or made it bigger, things began to work again. Strangely though, any other trigger of the same size didn't effect the player like the "buggy" one did. I guess it was all just a collision problem. Thank you to everyone that put in and helped! It was very appreciated! :P

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
1

Answer by Kiwasi · Jun 10, 2014 at 07:24 AM

I find a short period of invincibility immediately after hitting the trigger works well. You can do this by setting a bool on either the trigger or 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 MrRandomFella · Jun 10, 2014 at 10:32 AM 0
Share

Thanks for the idea of invincibility after getting hit, I'll have to look into this :D

avatar image
0

Answer by screenname_taken · Jun 10, 2014 at 07:07 AM

Perhaps you have an animation causing the trigger to trigger multiple times? Like a stumble or a knockback animation that makes the trigger move out and then back in.

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 MrRandomFella · Jun 10, 2014 at 10:09 AM 0
Share

Hmmm, maybe that is the cause, I'll remove the animation and get back with the result. :)

avatar image MrRandomFella · Jun 10, 2014 at 10:12 AM 0
Share

Ok, still nothing. $$anonymous$$y player just seems to take the first hit successfully and the second hit destroys it. :/

avatar image
0

Answer by yigites · Jun 10, 2014 at 07:14 AM

Maybe you dublicated "Hazard" object and forgot to move so there may be 2 Hazard object inside each other.

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

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

26 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

Related Questions

Damage not triggering in build 1 Answer

Health below zero but shows negative numbers 1 Answer

Object Not Recieving Damage 3 Answers

Expecting ) found = 1 Answer

Enemy Health Damage 2 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