Temmie Temmie - 1 month ago 17
C# Question

How to prevent the gameObjects being destroyed with the same script on Collision?

All of the gameObjects(enemies) get destroyed when the player press attack on one of the game objects. I want it to destroy the gameObject(enemy) if it collided with the current ones while the others won't get destroyed. All of the enemies have the same script.



Here is the Movement script where it is attached in the MainCharacter gameObject..


public class Movement : MonoBehaviour
{
public float movementSpeed = 6.0f;
public GameObject player;
public int jumpHeight = 350;
private bool onGround = false;
private bool afterMovingPlatform = false;
Animator anim;

//// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
player = GetComponent<GameObject>();
}

void Update()
{
//these are the codes for keyboard inputs when walking, attacking, etc...
}

void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "Ground")
{
onGround = true;

if (afterMovingPlatform)
{
transform.parent = null;
afterMovingPlatform = false;
}
}

if ((coll.gameObject.tag == "Respawn" || coll.gameObject.tag == "Enemy") && (this.anim.GetCurrentAnimatorStateInfo(0).IsName("Attack Right") || this.anim.GetCurrentAnimatorStateInfo(0).IsName("Attack Left")))
{
EnemyHealth.giveDamage();
}
else if (coll.gameObject.tag == "Respawn" || coll.gameObject.tag == "Enemy")
{
CoinScore.score = 0;
Health.health = 3;
EnemyHealth.enemyHealth = 1;
SceneManager.LoadScene("LevelF");
Debug.Log("RESPAWN");
}

if (coll.gameObject.tag == "MovingPlatform")
{
onGround = true;
afterMovingPlatform = true;
transform.parent = coll.transform;
}
}

//void onCollisionExit2D(Collision2D coll)
//{
// Debug.Log("EXIT");
// if (coll.gameObject.tag == "MovingPlatform")
// {
// transform.parent = null;
// }
//}
}



Script attached to the enemies. All of them have Enemy Healths: 1 inputted in unity.



using UnityEngine;
using System.Collections;

public class EnemyHealth : MonoBehaviour {

public static int enemyHealth = 1;
public Transform explosion;
public int enemyHealths;
CircleCollider2D coll;

// Use this for initialization
void Start () {
coll = GetComponent<CircleCollider2D>();
}

// Update is called once per frame
void Update()
{
enemyHealths = enemyHealth;
if (enemyHealths <= 0)
{
Instantiate(explosion, transform.position, transform.rotation);
Destroy(coll.gameObject);
}
}

public static void giveDamage()
{
enemyHealth -= 1;
}
}

Answer

Simply because the function giveDamage is declared as static (so does the enemyHealth member)

Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object.

Thus, every enemies share the same health amount, and when you damage one, in fact, you damage all the enemies.

Every enemy must have its own health. Remove the static keywords and do as follow in your player script

if ((coll.gameObject.tag == "Respawn" || coll.gameObject.tag == "Enemy") && (this.anim.GetCurrentAnimatorStateInfo(0).IsName("Attack Right") || this.anim.GetCurrentAnimatorStateInfo(0).IsName("Attack Left")))
{
    coll.gameObject.GetComponent<EnemyHealth>().giveDamage();
}
Comments