Deleting specific object with the same script running on other objects..

hello everyone… im new to unity…
i have an aiscript attached to all of my enemies, it works fine if there is only one enemy in the map.
but say for example there were 2 monsters spawned at the same time, if i attack monsterA and killed him, monsterB is also destroy…

how can i make it that only monsterA will be destroyed?
Do i have to make individual scripts for each enemy?
thank you…

can you post the code you’re using on the enemies? I think we need more details to help out.

Are you misusing statics or doing something like finding all enemies and destroying them?

Im sorry for the late reply… Don’t have an internet connection at home…
here’s my code for my enemy…

// this if for their movement and attack

var speed = 20.0;
var rotationSpeed = 50.0;
var shootRange = 250;
var attackRange = 300.0;
var shootAngle = 90.0;
var dontComeCloserRange = 300.0;
var delayShootTime = 0.35;
var pickNextWaypointDistance = 2.0;
var target : Transform;
var bullet : Rigidbody;
private var lastShot = -10.0;
static var enemyStateMachine : int;
static var myTransform : Transform;

// Make sure there is always a character controller
@script RequireComponent (CharacterController)
function Awake()
{
target = GameObject.FindWithTag(“Player”).transform;

}
function Start () {
// Auto setup player as target through tags
myTransform=transform;
enemyStateMachine=0;
//if (target == null GameObject.FindWithTag(“Player”))
//target = GameObject.FindWithTag(“Player”).transform;

//CanSeeTarget();
AttackPlayer();

}
function CanSeeTarget () : boolean {
//if (Vector3.Distance(transform.position, target.position) > attackRange)
//return false;

var hit : RaycastHit;
if (Physics.Linecast (transform.position, target.position, hit))
return hit.transform == target;

}

function AttackPlayer () {
var lastVisiblePlayerPosition = target.position;
while (true) {
if (true) {
// Target is dead - stop hunting
if (target == null)
return;

// Target is too far away - give up
var distance = Vector3.Distance(transform.position, target.position);
// if (distance > shootRange * 3)
// return;

lastVisiblePlayerPosition = target.position;
if (distance > dontComeCloserRange)
MoveTowards (lastVisiblePlayerPosition);
else
RotateTowards(lastVisiblePlayerPosition);

var forward = transform.TransformDirection(Vector3.forward);
var targetDirection = lastVisiblePlayerPosition - transform.position;
targetDirection.y = 0;

var angle = Vector3.Angle(targetDirection, forward);

// Start shooting if close and play is in sight
if (distance < shootRange angle < shootAngle)
{
//yield (“Shoot”);//print(“Shooting”);

enemyStateMachine=1;
// myTransform.position.y=25;
var tempBullet : Rigidbody;
x=myTransform.position.x;
y=myTransform.position.y;
z=myTransform.position.z;
tempBullet=Instantiate(bullet,Vector3(x,y,z),transform.rotation);
yield WaitForSeconds(1);
enemyStateMachine=0;
yield WaitForSeconds(3);
enemyStateMachine=1;
ElfElement.monsterElement=4;
}
} else {
enemyStateMachine=0;

// Player not visible anymore - stop attacking
if (!CanSeeTarget ())
return;
}

yield;
}
}

function RotateTowards (position : Vector3) {
// SendMessage(“SetSpeed”, 0.0);

var direction = position - transform.position;
direction.y = 0;
if (direction.magnitude < 0.1)
return;

// Rotate towards the target
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);
}

function MoveTowards (position : Vector3) {
var direction = position - transform.position;
direction.y = 0;
if (direction.magnitude < 0.5) {

//SendMessage(“SetSpeed”, 10.0);
return;
}

// Rotate towards the target
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);

// Modify speed so we slow down when we are not facing the target
var forward = transform.TransformDirection(Vector3.forward);
var speedModifier = Vector3.Dot(forward, direction.normalized);
speedModifier = Mathf.Clamp01(speedModifier);

// Move the character
direction = forward * speed * speedModifier;
//print(speedModifier);
GetComponent (CharacterController).SimpleMove(direction);
//animation.CrossFade(“run”);
//SendMessage(“SetSpeed”, speed * speedModifier, SendMessageOptions.DontRequireReceiver);
}


// this is for their health

static var enemyHealth : int=100;
var pickup : Transform;
static var showHealth : boolean=false;
static var counter : float;
var HealthStyle : GUIStyle;
var HealthPanel : GUIStyle;
var maxHealth : float=1000;
var curHealth : int =1000;
var healthbarLength : float;
static var damage : int;

function Awake()
{
enemyHealth=100;

}
function Start () {
healthbarLength=Screen.width/4;

}
function Update () {
AdjustCurrentHealth(damage);
damage=0;
if(enemyHealth<=0)
{

Instantiate(pickup,Vector3(transform.position.x,1,transform.position.z),transform.rotation);
PlayerScore.score+=10;
Destroy (gameObject);

//Application.LoadLevel(2);
}
/*else
{
print(enemyHealth);

}*/
}

function OnGUI()
{
if(showHealth)
{

GUI.Label(new Rect(0,600, 357, 35),“”,HealthPanel);
GUI.Label(new Rect(22,605,healthbarLength,25 ),enemyHealth+“/”+maxHealth,HealthStyle);

counter+=Time.deltaTime;
if(counter>5)
{
showHealth=false;
counter=0;
}
}

}
function AdjustCurrentHealth(adj)
{
enemyHealth += adj;
//print("receive damage of "+adj);
if(enemyHealth<=0)
enemyHealth=0;
if(enemyHealth>maxHealth)
enemyHealth=maxHealth;
if(maxHealth<1)
maxHealth=1;
healthbarLength=(Screen.width/4) * (enemyHealth / maxHealth);

}


// this one determines the damage im doing to them

static var elementType : int;
static var monsterElement : int;
//1-Fire
//2-Water
//3-Wind
//4-Earth
//5-Ice
//6-Lightning
function Update ()
{
switch (monsterElement)
{
case 1://case fire
fire();
break;
case 2://water
Water();
break;
case 3://Wind
Wind();
break;
case 4://Earth
Earth();
break;
case 5://Ice
Ice();
break;
case 6://Lightning
lightning();
break;

}
}

function fire()//monster is Fire Type
{
switch(elementType)//switch skill
{
case 1://fire vs fire
sameElement();
break;
case 2://water vs fire
greatDamage();
break;
case 3://wind
averageDamage();
break;
case 4://Earth
averageDamage();
break;
case 5://Ice
sameElement();
break;
case 6://Lightning
averageDamage();
break;

}
}
function Water()//monster is Water Type
{
switch(elementType)//switch skill
{
case 1://fire
noDamage();
break;
case 2://water
sameElement();
break;
case 3://wind
greatDamage();
break;
case 4://Earth
averageDamage();
break;
case 5://Ice
sameElement();
break;
case 6://Lightning
averageDamage();
break;

}
}
function Wind()//monster is Wind Type
{
switch(elementType)//switch skill
{
case 1://fire
greatDamage();
break;
case 2://water
noDamage();
break;
case 3://wind
sameElement();
break;
case 4://Earth
sameElement();
break;
case 5://Ice
sameElement();
break;
case 6://Lightning
averageDamage();
break;

}
}
function Earth()//monster is Earth Type
{
switch(elementType)//switch skill
{
case 1://fire
sameElement();
break;
case 2://water vs fire
averageDamage();
break;
case 3://wind
averageDamage();
break;
case 4://Earth
sameElement();
break;
case 5://Ice
greatDamage();
break;
case 6://Lightning
noDamage();
break;

}
}
function Ice()//monster is Ice Type
{
switch(elementType)//switch skill
{
case 1://fire
averageDamage();
break;
case 2://water
sameElement();
break;
case 3://wind
averageDamage();
break;
case 4://Earth
noDamage();
break;
case 5://Ice
sameElement();
break;
case 6://Lightning
greatDamage();
break;

}
}
function lightning()//monster is lightning Type
{
switch(elementType)//switch skill
{
case 1://lightning vs fire
averageDamage();
break;
case 2://waterskill
sameElement();
break;
case 3://windSkill
averageDamage();
break;
case 4://Earth
greatDamage();
break;
case 5://Ice
noDamage();
break;
case 6://Lightning
sameElement();
break;

}
}

function noDamage()
{
EnemyHealthScript.damage=0;
EnemyHealthScript.showHealth=true;
elementType=0;

}
function averageDamage()
{
EnemyHealthScript.damage-=5;
EnemyHealthScript.showHealth=true;
elementType=0;

}
function sameElement()
{
EnemyHealthScript.damage-=1;
EnemyHealthScript.showHealth=true;
elementType=0;

}
function greatDamage()
{
EnemyHealthScript.damage-=15;
EnemyHealthScript.showHealth=true;
elementType=0;

}

@JRavey

i created spawn points around the map and whenever i get close to that spawn points it summons monsters…
Now for example i ran into a spawn point and then ran into another one, that summons two monsters with the same scripts…
If i killed the first monster the second monster is also destroyed even though im not attacking it. Is it because they have the same script?

thanks guys!!!

I believe you may want to modify these lines:

static var enemyHealth : int=100;
...
static var damage : int

Think about how that would affect:

function Update () {
AdjustCurrentHealth(damage);
damage=0;
if(enemyHealth<=0)

As it is, every instance of this script shares those variables. A bit of a problem when that static damage is applied to everybody and they share one value for health.

As aside, thank you for actually posting code, usually people don’t do that. Remember the code tags to make it easier to read.

@JRavey
ok i understood what you said, but how will i access those variables from other script?

i made them static because they are being access by other objects(which is the my character’s skills)…
thank you…