So right now I have 5 enemies in my game and I have an array with them in it for randomized positions. This is all C# scripts by the way. Since I have multiple enemies I am wondering how I can give each enemy their own health bar and variables/values so when I melee attack them their health and bars decrease separately. Before I had 5 enemies I could attack them and they would be damaged but now that I have 5 enemies I can’t even hurt them. Please help!
in the actual editor, look at your Hierarchy
try to actually ADD the health bar system, underneath your enemy thing.
ideally, you should just have the one enemy (it’s like the “perfect model enemy”)
then to make your five, when the game begins, use instantiate to make five of them!!
when you approach life like this you can just add ANYTHING to the “model enemy” when you’re working. then … it will automatically do that for all of the enemies. awesome.
for instance if you need to add a “green flashing light” just go ahead and add it to your one “model enemy”. when you instantiate the five of them in to the array - you make five copies of your “model” enemy - there you go, they all have green flashing lights.
in this way it will be super-easy to add the health bars (or, anything else).
here’s a little bit of code that takes your “model” enemy and makes a dozen of him in an array. but you can do it any way you wish. here, the array holds the actually SCRIPT attached to your enemy - it could just be the transform if you prefer. (PS once you get going, never use an array, actually use a List!)
///////////////////////////////////////////////////////////
private var model:Transform;
// just connect that to your model somehow...
private var ee:EnemyLame[];
function Start()
{
ee = new EnemyLame[12]; // the script on the enemy is called 'EnemyLame'
for ( var i:int=1; i<=11; ++i )
{
var newy:GameObject = Instantiate( model.gameObject );
newy.transform.parent = transform;
newy.transform.localPosition = Vector3.zero;
var rr:float;
rr=Random.Range(-3.0,3.0);
newy.transform.localPosition.x += rr;
rr=Random.Range(-3.0,3.0);
newy.transform.localPosition.y += rr;
newy.name = "ES" + i;
ee *= newy.GetComponent(EnemyLame);*
-
}*
- }*
///////////////////////////////////////////////////////////
Holy cow, thanks so much for the response but we have one problem. I am using C# scripts and I can’t use JS’s. You see, I am only allowed to use C# scripts. Would you happen to have an C# alternative or equal?