- Home /
How to make an enemy change tags after death.
Hi guys! My Unity3D Game is coming along nicely. Learning a bunch on the way! I was wondering is there anyway to make and enemy change its tag from "enemy" to "untagged" the enemy AI Script i have uses Health ofc is there a way to make it change tags if health reaches 0. The reason i want to achieve this is because the dead bodys are screwing my radar. If i could get any info on this it would be muchly appreciated. Thank you all in advance.
Answer by syclamoth · Jul 05, 2012 at 11:35 AM
Very straightforward:
gameObject.tag = "";
This will remove the tag from the gameObject. Just drop this into your 'death' function.
Looks promising.... Only bad thing.. i cant find the death function now lolz... thanks for the help.
Answer by Linus · Jul 05, 2012 at 11:35 AM
See http://docs.unity3d.com/Documentation/ScriptReference/GameObject-tag.html
After:
dead= Instantiate(deadReplacement2, GOPos.transform.position, GOPos.transform.rotation);
Insert:
dead.tag = 'newtag';
Tags must be declared in the tag manager before using them.
Answer by AusAndrew19 · Jul 05, 2012 at 12:00 PM
ShotHead=true;
}
function ApplyDamage (damage : float) {
if (hitPoints <= 0.0)
return;
// Apply damage
hitPoints -= damage;
scoreManager.DrawCrosshair();
// Are we dead?
if (ShotHead==true) {hitPoints=0.0;Replace();}
else if (ShotHead==true && hitPoints >= 0.0) {hitPoints=0.0;Replace();}
else if (ShotHead==false && hitPoints <= 0.0)Replace();
}
function Replace() {
// If we have a dead barrel then replace ourselves with it!
if (deadReplacement && ShotHead==true) {
dead= Instantiate(deadReplacement2, GOPos.transform.position, GOPos.transform.rotation);
scoreManager.addScore(20);
// For better effect we assign the same velocity to the exploded barrel
dead.rigidbody.velocity = rigidbody.velocity;
dead.angularVelocity = rigidbody.angularVelocity;
Destroy(gameObject);
}
else {
dead=Instantiate(deadReplacement, GOPos.transform.position, GOPos.transform.rotation);
scoreManager.addScore(20);
// For better effect we assign the same velocity to the exploded barrel
dead.rigidbody.velocity = rigidbody.velocity;
dead.angularVelocity = rigidbody.angularVelocity;
Destroy(gameObject);
}
// Destroy ourselves
}
is my damage script. But i cannot seem to find where to place this little code.
gameObject.tag = "";
This can be done to the deadReplacement, attach a script (or edit an existing one) to the replacement prefab, and place the code there in an Awake function. Or see my updated answer
And please edit your code posted here into your question posting. And make sure to turn on code formatting on the code to make it easier to read.