My TakeDamage function doesnt work
Hello everyone, am stuck with my project. When i call my TakeDamage function, the health slider doesn't get reduced. Here is my code and screen shoots.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
namespace s3
{
public class PlayerHealth : MonoBehaviour {
public int StartingHealth = 100;
public int CurrentHealth;
public Slider HealthSlider;
// public Image DamageImage;
// public float FlashSpeed = 5f;
// public Color FlashColor = new Color(1f, 0f, 0f, 0.1f);
// public TankShooting PlayerShooting;
// public CarController CarControl;
bool IsDead;
bool Damaged;
// Use this for initialization
void Awake () {
CurrentHealth = StartingHealth;
HealthSlider = GetComponent<Slider> ();
// PlayerShooting.GetComponent<TankShooting> ();
// CarControl = GetComponent<CarController> ();
}
public void TakeDamage (int amount)
{
CurrentHealth -= amount;
HealthSlider.value = CurrentHealth;
if (CurrentHealth <= 0 && !IsDead)
{
Death ();
}
}
void Death ()
{
// PlayerShooting.enabled = false;
// CarControl.enabled = false;
}
void Start () {
}
// Update is called once per frame
void Update () {
}
}
}
This is the code where the TakeDamage function is called
![using UnityEngine;
using UnityEngine.UI;
namespace s3
{
public class TankShooting : MonoBehaviour
{
public int m_PlayerNumber = 1;
public Rigidbody m_Shell;
public Transform m_FireTransform;
public AudioSource m_fireSound;
public ParticleSystem m_muzzleFlashTurret;
public Light m_light;
PlayerHealth playerHealth;
public int damage;
GameObject player;
private string m_FireButton;
private float m_CurrentLaunchForce = 6000f;
private bool m_Fired;
private void OnAwake()
{
m_fireSound = GetComponent<AudioSource>();
m_Shell = GetComponent<Rigidbody>();
m_light = GetComponent<Light> ();
player = GameObject.FindGameObjectWithTag ("Cube");
playerHealth = player.GetComponent<PlayerHealth>();
}
private void Start()
{
m_FireButton = "Fire" + m_PlayerNumber;
m_light.enabled = false;
}
private void Update()
{
// Track the current state of the fire button and make decisions based on the current launch force.
if(Input.GetButtonDown (m_FireButton)) {
playerHealth.TakeDamage (damage);
Fire();
m_light.enabled = true;
}
if (Input.GetButton (m_FireButton)) {
Fire();
m_light.enabled = true;
}
if (Input.GetButtonUp (m_FireButton)) {
m_light.enabled = false;
}
}
private void Fire()
{
// Instantiate and launch the shell.
m_Fired = true;
Rigidbody shellInstance = Instantiate (m_Shell, m_FireTransform.position, m_FireTransform.rotation) as Rigidbody;
// shellInstance.AddForce (m_FireTransform.forward * 6000*Time.deltaTime);
shellInstance.velocity = m_CurrentLaunchForce*m_FireTransform.forward*Time.deltaTime;
// play fireshoot audio
// if(m_fireSound.isPlaying) m_fireSound.Stop();
// m_fireSound.Play();
// play muzzle flash when turret is fired
if(m_muzzleFlashTurret.isPlaying) m_muzzleFlashTurret.Stop();
m_muzzleFlashTurret.Play();
// playerHealth.TakeDamage (damage);
}
}
}][1]
[1]: /storage/temp/68505-screenshot-53.png
screenshot-51.png
(297.5 kB)
screenshot-53.png
(269.8 kB)
Comment