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 if
s 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");
}
}