- Home /
When I kill one of the two enemies, both dies
I have two of the same enemies, that has the same amount of health and they have the same scripts attached because they are both skeletons But when I kill one, both dies. How can I fix it, because I dont want to make a new scripted for each enemy if they are the same.
You are using a static variable for health - you should only use static variables when you really know what you are doing - there are articles describing how to get the right script on the right object available on Unity Gems and this article covers this...
Yeah the articles on Unity Gems are pretty good. The site covers coding in a way many other tutorials should. I actually came across a tutorial series where right during the second or third video the guy said we must declare some static variables because this way we can access them from other scripts.. I was like wtf, I'm a C# developer for years now, and if this makes sense in Unity then it's really bad news.. and it's not the only tut like this I've seen.. I think the biggest problem with getting into game development vs. getting into other areas of computer science is many people just skip the phase of taking the time to learn program$$anonymous$$g. I'm also not completely in peace with Javascript.. it hides so much important detail, and makes aspiring game programmers write sloppy code, especially beginners.. which is not a trade-off I$$anonymous$$O.
Answer by NaturedOne · Jan 10, 2013 at 10:51 AM
Have you tried duplicating the script for each skeleton and attaching a separate copy? Do you still get the same results?
Have you made sure that the health pool for the "skeletons" is not static?
Hopefully this helps! Best of luck.
Answer by Piflik · Jan 10, 2013 at 10:50 AM
Don't use static variables unless you really need to or know what they do. Static variables belong to the class, not the objects, so there is only ever one single instance of a static variable. Static is not a keyword for easy access.
Answer by cdrandin · Jan 10, 2013 at 10:51 AM
If you make a enemy class, each class variable is independent of another.
class Enemy ( MonoBehavior ):
public health as single = 30.0F
def hit ():
health -= 10.0
//In another script just create a new instance of the class
//soo...
mob1 = Enemy ()
mob2 = Enemy ()
//or make an array.. for boo it is..
mobs as (Enemy)
//then do the hit and damage is applied to each unique //instance of the class Enemy
You should mention that your code is written in Boo because almost no one use it or can read it ;)
lol it is derived by python which I can gaurntee its philosophy is easy reading of code :P
Your answer
Follow this Question
Related Questions
How to stop enemies dying on spawn 1 Answer
Instantiation array of enemies, c# ? 2 Answers
Arrays of Enemies in a Game HELP! 0 Answers
I can't have more enemies at the same time. 1 Answer
How can i make a ray cast take health from enemies 2 Answers