- Home /
Ranged damage not working
Below is my enemyHealth code
using UnityEngine;
using System.Collections;
public class enemyHealth : MonoBehaviour
{
public int maxHealth = 100;
public float currHealth = 100;
public float healthBarLength;
// Use this for initialization
void Start () {
healthBarLength = Screen.width / 4;
}
// Update is called once per frame
void Update () {
AdjustCurrentHealth(0);
}
void OnGUI() {
GUI.Box(new Rect(10, 40, healthBarLength , 20), currHealth + "/" + maxHealth);
}
public void AdjustCurrentHealth(int adj) {
currHealth += adj;
if(currHealth < 1)
currHealth = 0;
if(currHealth > maxHealth)
currHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
healthBarLength = (Screen.width / 4) * (currHealth / (float) maxHealth);
}
public void TakeDamage(int amount)
{
currHealth -= amount;
}
}
Below is my projectile code
using UnityEngine; using System.Collections;
public class Projectile : MonoBehaviour
{
public int Damage;
public GameObject target;
public void OnCollisionEnter(Collision collision)
{
enemyHealth eh = (enemyHealth)target.GetComponent("enemyHealth");
eh.AdjustCurrentHealth(- Damage);
}
}
I can't figure out what is wrong when I shoot them them their health doesnt decrease by 10 which is what i set the damage as.
Please take a second and make your post more legible.
ensure your code is properly formatted
add indentation to your code so people can understand it
consider adding a sentence to explain exactly what it is you're after that isn't hidden at the end as a code comment
there's no need to tag with "help"; that's the whole purpose of this site
i tried my best to format it properly by tinkering around with editer lol
Tried your line and got this error
Assets/C#Scripts/Projectile.cs(11,17): error CS0266: Cannot implicitly convert type UnityEngine.Component' to
enemyHealth'. An explicit conversion exists (are you missing a cast?)
sorry it should have probably been
enemyHealth eh =(enemyHealth)collision.gameObject.GetComponent("enemyHealth");
Answer by Blankzz · Jul 22, 2011 at 10:28 PM
Try enemyHealth eh = (enemyHealth)collision.gameObject.GetComponent("enemyHealth");
instead of
enemyHealth eh = (enemyHealth)target.GetComponent("enemyHealth");
Your answer
Follow this Question
Related Questions
Health Regeneration 2 Answers
Health Bar fire damage 1 Answer
Universal Damage Sytem 2 Answers
Enemy Health, Player Damage 0 Answers
Damage/Health problem 2 Answers