Why is this script not working?
the script is meant to take away Enemy health and eventually destroy the enemy, however it does not work, and instead, I would be randomly shhoting an enemy, (NOT where the Target is,) and it would loose health, which is not what i want. the script is here:using UnityEngine; using System.Collections; using UnityEngine.UI;
public class Enemyhealth : MonoBehaviour {
public static int health = 1000;
public GameObject Bot;
public Slider healthBar;
// Use this for initialization
void Start () {
InvokeRepeating("LooseHealth",1,1);
}
// Update is called once per frame
void LooseHealth() {
var laser1 = GameObject.FindWithTag("laser1");
var laser2 = GameObject.FindWithTag("laser2");
var Enemy = GameObject.FindWithTag ("bot");
if (laser1 == GameObject.FindWithTag("Enemy").transform)
{
health = health - 1000;
}
if (laser2 == GameObject.FindWithTag("Enemy").transform)
{
health = health - 1000;
}
healthBar.value = health;
if (health <= 0)
{
Destroy (Bot);
}
}
}
any help would be appreciated
It's hard to say exactly what edits to make without knowing a few more details about your project. Is this script attached to each enemy instance or to a game manager?
In any case, you might find it useful to do the Space Shooter tutorial which covers how to do this sort of thing.
@Lewance it is basically a 3d game in which you are a first person character able to shoot lasers from your hands, almost like Iron $$anonymous$$an, and you are to shoot robots,(Spheres with guns on their heads,) until they die, and they are not really dying
okay but what is this script attached to? if you don't know how to answer that question then I really recommend doing the tutorial i linked above
Answer by Lewance · Jun 05, 2016 at 02:27 PM
Okay, if it's attached to the bot then you don't need to use: GameObject.FindWithTag("Enemy").transform That function will find an object from the heirarchy that's tagged as an enemy and if that enemy is being hit by a laser then the rest of your code will cause damage to be taken by this bot.
Instead you can just say "transform", you're already on the right game object! i.e.
if (laser1.position == transform.position)
{
health -= 1000;
}
Your answer
Follow this Question
Related Questions
Enemy character disappears as soon as i hit play 0 Answers
i try make enemy health slider or any uI just show the health of Enemy Over head 0 Answers
Damage not working properly 1 Answer
Enemy dies in editor mode but not in build 2 Answers
How to make health and damage script function correctly? 3 Answers