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;
}
}
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;
}
}
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.
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
thanks so much, I do need to get better at this ,thanks its helped me realize.
Your answer

Follow this Question
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