Need help with C# code. both gameObject and col.gameObject are getting destroyed. here is the code.

i have a few more if statements just like this one obviously changing the tags and player number each time. also is there a more efficient way of doing this. thanks

if (player[0] && (col.gameObject.tag == "2" || col.gameObject.tag == "3" || col.gameObject.tag == "4")) 
{ 
Destroy (gameObject); 
} 
if (player[0] && col.gameObject.tag == "1") 
{ 
Destroy(col.gameObject); 
}

Assuming player array is an array of Unity Objects, right now your code checks

if (player[0] && (col.gameObject.tag == "2" || col.gameObject.tag == "3" || col.gameObject.tag == "4")) 

if the object in player[0] is not destroyed/null, and the colliding object has tag 2, 3, or 4 => destroy this gameobject.

after that you do

if (player[0] && col.gameObject.tag == "1") 

if the object in player[0] is not destroyed/null, and the colliding object has tag 1 => destroy the other gameobject.

There’s no else connecting the two ifs so you always do both checks.

if you(hero) hit same shape other shape(enemy) gets destroyed, if you hit different enemy, hero gets destroyed.

You can write this system more simply. Your are checking shapes so it would be a good starting point to make an object you can use to describe shapes. For example

public class Shape : MonoBehaviour {
    public enum ShapeType {
        Square,
        Circle,
        Triangle,
        Pentagon,
    }

    public ShapeType type;
}

Attach that to all prefabs that need to have a shape and use the dropdown in the inspector to set the desired shape to all of them.

After that you can write the condition code almost like written text.

var playerShape = player[0].gameObject.GetComponent<Shape>();
var otherShape = col.gameObject.GetComponent<Shape>();

if (otherShape != null && playerShape != null) {
    if (myShape == otherShape) { // if player and colliding shape are the same
        Destroy(otherShape.gameObject);
    } else {
        Destroy(gameObject);
    }
} else { 
    // this is just to check whether you remembered to attach Shape scripts 
    if (otherShape == null) {
        Debug.LogError(col.gameObject.name + " did not have a 'Shape' Component");
    } else {
        Debug.LogError(player[0].gameObject.name + " did not have a 'Shape' Component");
    }
}

50 lines of code cut down to this, thank you @NoseKills for placing the idea into my head.

 void OnCollisionEnter2D (Collision2D col) {

        if (gameObject.tag == col.gameObject.tag) {
            Destroy(col.gameObject);
        }
        else { Destroy(gameObject); }        
    }

you dont know how happy i am. thanks man