How to make unity recognice tags?
I have tried once and again to make unity understand that the enemy is NOT colliding with the correct object, but, after many changes, the debug.log ("va") is still triggering even when the enemy is not colliding with the "muro". Please, please, help me with this if you know the answer. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class enemy_2 : MonoBehaviour {
public ColliderDistance2D colldist;
public Collider2D squareCollider;
public float Delay;
public float timer;
public bool FacingRight = true;
public Vector2 direction;
public GameObject Player;
public GameObject pared;
public GameObject muro;
public int EnemySpeed;
public int XmoveDirection;
void Start ()
{
Delay = 3;
squareCollider = pared.GetComponent<Collider2D> ();
muro = GameObject.FindWithTag ("muro");
pared = GameObject.FindWithTag ("pared");
}
void FixedUpdate ()
{
timer += Time.deltaTime;
if (timer > Delay)
{
colldist = gameObject.GetComponent<Collider2D> ().Distance (squareCollider);
Vector3 direction = new Vector2 (XmoveDirection, 0.0f);
gameObject.GetComponent<Rigidbody2D> ().velocity = direction * EnemySpeed;
if (colldist.distance < 2 && FacingRight == false && pared)
{
Flip ();
}
if (colldist.distance < 2 && FacingRight == true && muro)
{
Debug.Log ("va");
InverseFlip ();
}
if (GetComponent<Collider2D>().tag == "Player")
{
Destroy (Player.gameObject);
}
}
}
void Flip ()
{
FacingRight = !FacingRight;
Vector2 localScale = gameObject.transform.localScale;
localScale.x *= -1;
transform.localScale = localScale;
XmoveDirection = -1;
}
void InverseFlip()
{
FacingRight = FacingRight;
Vector2 localScale = gameObject.transform.localScale;
localScale.x *= -1;
transform.localScale = localScale;
XmoveDirection = 1;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612142117im_/https://answers.unity.com/themes/thub/images/avi.jpg)