- Home /
GetComponent Picking up multiple scripts from differrent objects?
I have a player who shoots a raycast at a target, the raycast hits this target and returns its tag. The object hit has multiple copies of itself from the same prefab (its a target board to shoot at) and each copy has it's own script which has a function which deducts health when it is shot.
The gun's code:
if (Physics.Raycast(Spawn.position, direction, hit, range)) {
if (hit.collider.tag == "target") {
var targ : GameObject = hit.transform.gameObject;
targ.GetComponent(target).takeHealth(5);
The target's code:
static var health = 50;
function takeHealth(mod) {
health = health - mod;
isDead();
}
function isDead() {
if (health <=0) {
Destroy(gameObject);
}
}
My problem is, if I destroy one of these targets, all of the target's healths will be deducted. How do I stop my game from deducting the health from all of them and only the one i am shooting at?
Answer by aldonaletto · Oct 24, 2012 at 11:54 PM
Just remove the static keyword: it's declaring that health is the same variable for all scripts, but what you need is a local health in each target (non-static).
Your answer
Follow this Question
Related Questions
Problem with raycast shooting 1 Answer
how to make one script change a variable in another scipt 2 Answers
AddComponent can't find script name MouseOrbit? 1 Answer
Send Message? 1 Answer