- Home /
Two Instances of same Prefab dealing damage to each other.
So, I have two instances of the same Prefab and when one of them attacks the other, both the GameObjects takes damage.
The Prefab is loaded with:
character1 = Resources.Load(<prefab path>) as GameObject;
The instances are created with:
player = Instantiate (character1, playerSpawn.position, playerSpawn.rotation);
And the damage is applied with a Trigger Collider, with:
void OnTriggerEnter2D(Collider2D other) {
if (other.gameObject.tag == "Player") {
other.SendMessage ("SetDamage", 10);
}
}
The Trigger Collider starts disabled. When the player presses the attack button, it becomes enabled and, after a short period of time, disabled again.
The problem is: when I have two character1
on the scene and one attacks another, both the instances take the damage.
I've made some testing with other characters. If there are two character1
and one character2
(another Prefab), and the character2
deals damage to one of the character1
, only that instance takes damage, and not both.
Also, when one instance of character1
hits the other from behind, both still take damage. The Trigger Collider is located right in front of the GameObject, which means that the Collider of the instance that was hit from behind was not touching the instance that attacked.
Any toughts are welcome.
EDIT: I've added a new bool enable$$anonymous$$elee
and setted it true right after when I enable the Collider and false when I disable it. I've also changed the the OnTriggerEnter2D function to:
void OnTriggerEnter2D(Collider2D other) {
if (other.gameObject.tag == "Player" && enable$$anonymous$$elee) {
other.Send$$anonymous$$essage ("SetDamage", 10);
}
}
Now it works, but the two instances still call the OnTriggerEnter2D function and I don't know why. Still wanna solve this problem