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 Po-Man · Oct 19, 2015 at 04:07 PM · script.

Can someone help me with my script?

Hello, I have this script that I made for health it sort of works but the problem is that each time the players health reaches 0 the components are enabled and disabled over and over again.

is this because the death code is in a update method or what? here is the code...

using UnityEngine; using UnityEngine.UI; using System.Collections;

public class PlayerHealth : MonoBehaviour { public MeshRenderer skinOfPlayer; public PlayerController playerController; public int startingHealth = 100; bool isDead;

 void Start () 
 {

 }
 

 void Update ()
 {
     if (startingHealth == 0) 
     {
         isDead = true;
     }

     if (isDead == true) 
     {
         playerController.enabled = !playerController.enabled;
         skinOfPlayer.enabled = !skinOfPlayer.enabled;
     }
 }
 
 void OnTriggerEnter(Collider other)
 {
     startingHealth -= 10;
 }

}

Comment
Add comment · Show 5
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 Po-Man · Oct 18, 2015 at 09:53 PM 0
Share

Sorry for the poor coding

avatar image Po-Man · Oct 18, 2015 at 11:12 PM 0
Share

I found a way around this ins$$anonymous$$d of disabling two components of the player we could just disable the whole player at once.

using UnityEngine; using UnityEngine.UI; using System.Collections;

public class PlayerHealth : $$anonymous$$onoBehaviour { public GameObject player; public int startingHealth = 100; bool isDead;

 void Start () 
 {

 }
 

 void Update ()
 {
     if (startingHealth == 0) 
     {
         isDead = true;
     }

     if (isDead == true) 
     {
         player.SetActive(false);
     }
 }
 
 void OnTriggerEnter(Collider other)
 {
     startingHealth -= 10;
 }

}

avatar image Po-Man Po-Man · Oct 18, 2015 at 11:16 PM 0
Share

What I was trying to do was make the player moving script and player mesh renderer get disabled when the player health is 0 ,it makes it much easier if you just disable the whole gameobjcet.

avatar image SRPent · Oct 20, 2015 at 12:54 AM 0
Share

Going to point out some stuff in your code to help you get better. ;)

When using health or energy like stuff better use less than rather that is equal to, this happens because it usually is like 0.00000001 or 0.1 if you add other factors or maybe it is like 1-10 (-9) so it's not 0. (so use less than 1)(saying this more like a good practice)

Other thing that I can note is that when it collides with anything (you're not specifying what should it collide with) it takes 10 points away each frame. (make a boolean that checks if it is being hit, if it is only damage once (ins$$anonymous$$d of every frame)).

Have you declared startingHealth and isDead in that script? (bool isDead = false; float startingHealth = 100;)

Try to make a starting health and a health, this could be useful if you later decide to make something like go full health power-up stuff. (of maybe use it like a max health) And then you can work on health ins$$anonymous$$d of the startingHealth. (Only as a good practice)

You can leave line 14 as if(isDead){} (what if statements do is to let the condition pass if what is inside it is true, like 1+1==2 (that's true) then it'll let it be. Or if this = true is the same as if saying if this) (also good practice)

Then

if (startingHealth == 0) { isDead = true; } if (isDead == true) { playerController.enabled = !playerController.enabled; skinOfPlayer.enabled = !skinOfPlayer.enabled; }

You can make it shorter like this.

if (startingHealth == 0) { playerController.enabled = !playerController.enabled; skinOfPlayer.enabled = !skinOfPlayer.enabled; }

Because if line 9 is true, then line 14 will always be true.

I don't know exactly what the problem is, but it may solve it.

Only here to help. :'D

avatar image Po-Man SRPent · Oct 20, 2015 at 06:34 PM 0
Share

thanks so much, I do need to get better at this ,thanks its helped me realize.

0 Replies

· Add your reply
  • Sort: 

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

How to make brake car ? 2 Answers

How do i put "Wasted!" after respawn? 0 Answers

Have object fly towards camera 0 Answers

Animation only plays in one of the objects 1 Answer

Load scene after battle 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