This is the script, basically it detects objects near the player, in this case it’s trying to detect nearby objects. The tag I am detecting is called “Pushable Object”. When I use Debug.Log to print out the tag the RayCasts were detecting, they were printing “Pushable Object”, but when I try to compare it using another if statement inside with CompareTag, it stops working and says that the “Pushable Object” tag is not defined. Even if I try to use other methods by directly comparing the tag with a string, it still doesn’t work (though that doesn’t print out an error).
(Note: The tag is correctly set up in the project settings and assigned to the correct object)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerNearbyObjectDetection : MonoBehaviour
{
public bool pushableObjectAtLeft;
public bool pushableObjectAtRight;
public bool pushableObjectAtTop;
public bool pushableObjectAtBottom;
void Update()
{
RaycastHit2D leftRayCast = Physics2D.Raycast(transform.position, Vector2.left, 1f);
RaycastHit2D rightRayCast = Physics2D.Raycast(transform.position, Vector2.right, 1f);
RaycastHit2D topRayCast = Physics2D.Raycast(transform.position, Vector2.up, 1f);
RaycastHit2D bottomRayCast = Physics2D.Raycast(transform.position, Vector2.down, 1f);
if (leftRayCast.collider != null)
{
pushableObjectAtLeft = true;
}
else
{
pushableObjectAtLeft = false;
}
if (rightRayCast.collider != null)
{
Debug.Log(rightRayCast.collider.transform.gameObject.tag);
if (rightRayCast.collider.transform.gameObject.CompareTag("Pushable Object"))
{
Debug.Log("Correctly detected tag");
pushableObjectAtRight = true;
}
}
else
{
pushableObjectAtRight = false;
}
if (topRayCast.collider != null)
{
pushableObjectAtTop = true;
}
else
{
pushableObjectAtTop = false;
}
if (bottomRayCast.collider != null)
{
pushableObjectAtBottom = true;
}
else
{
pushableObjectAtBottom = false;
}
}
}
Is there any way I can fix this and have it detect the tag correctly?

