- Home /
Specific enemy variable affects all enemies
All the enemies in my game have health and on collision with the player, they subtract their current level of health from the players.
The problem is that if the player reduces one of their health to zero, all of them disappear. At the same time, if I set their health to private variable, the health won't sutbract from the enemies health on collision.
public var Health: int = 100;
function OnCollisionEnter(other: Collision){ if(other.gameObject.CompareTag("Player")){ other.gameObject.Health -= Health; Destroy(gameObject); rigidbody.velocity = -rigidbody.velocity; }
try using a public variable, we had similar problems and ours is now a public variable, however I did do a few other things which I can't recall (chopping and changing solutions got all mixed up) so it may or may not work. :(
That didn't work. It could call the Health from the other scripts.
This question is really confusing, almost every use if 'their' or 'health' is ambiguous.
You're reducing player health by enemy health each time an enemy collides with a player? The code looks straightforward. I don't understand your problem.
Answer by karl_ · Apr 05, 2011 at 04:21 PM
I'm not sure if this is exactly what you're looking for, but it should at least give you an idea as to how to go about modifying variables in other scripts without using static vars. If this script is attached to an enemy object any time it comes into contact with the player it will subtract it's current health from the player then self destruct.
http://unity3d.com/support/documentation/ScriptReference/Component.GetComponent.html
var enemyHealth : int;
function OnCollisionEnter(col : Collision) { if(col.gameObject.CompareTag("Player")) { var tempScript = col.gameObject.GetComponent("name of script that handles health"); tempScript.health -= enemyHealth; Destroy(gameObject); } }
Small error: var tempScript = col.GetComponent("name of script that handles health");
Is suppose to be: var tempScript = col.gameObject.GetComponent("name of script that handles health");
Answer by Justin Warner · Apr 02, 2011 at 03:01 PM
In Java, their are things called getter/setter methods, which are private, to access private variables...
So, try making it private, and then add in a setter method, that brings in the damage, and then subtract it and set it there.
Make sure it's not static too...
Um, can't think of anything else right now, but you can try this =).
But if it's not static, then other game objects can't interact with it.
Justin, I need for you to elaborate a bit more on it.
Static means there is one copy for ALL enemies. Public/private means "others can/can't see/change it."
Owen, I think you're thinking of it wrong... Static is the same for all people... So, if you have a static health, you kill one person, and it kills all of them... You need to just make it private, and keep it dynamic...
Same meaning. Static means one copy for the entire class, which means what you said -- if you zero out "your" health, you are really zeroing the single health we all share. Quoting your answer, above "$$anonymous$$A$$anonymous$$E SURE IT'S NOT STATIC." As long as it is public or has a public get, there won't be any "can't interact" issues.
Your answer
Follow this Question
Related Questions
Need Help With My GAME! 3 Answers
Script effects all gameobjects. 1 Answer
For loop on collision (two hit kill) 2 Answers
NullReferenceException: Object reference not set to an instance of an object 4 Answers