How do I refer to a tagged object without referring to a name.,Don't know how to reference an objects tag without knowing what object it is.

I’m trying to make an object that the script is applied to look at an object that enters the radius of the trigger. I found something that is not making compile errors but the if statement doesn’t work because I seem to be referring to nothing. The line “transform.LookAt(obj.transform);” is mainly what I’m talking about.

public class AttractObject : MonoBehaviour
{

public GameObject attractionTo;//The the thing the script is applied to
private TagField CanBePulledByGrav;
public float strengthOfAttraction = 5.0f;
private Boolean GravRaidus;//The trigger collider

// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    
}
    void OnTriggerEnter(Collider other)
    {
        Debug.Log("Object entered the trigger");
        if (other.tag == "CanBePulledByGrav")
        {
            GameObject obj = GameObject.Find("CanBePulledByGrav");
            transform.LookAt(obj.transform);
            Debug.Log("If is working.");
        }
    }

    void OnTriggerStay(Collider other)
    {
        //Debug.Log("Object is within trigger");
    }

    void OnTriggerExit(Collider other)
    {
        Debug.Log("Object exited the trigger");
    }

}

I’m not sure what you’re trying to achieve.

In your OnTriggerEnter method, you’re checking the colliding game object’s tag to see if its value is “CanBePulledByGrav”, and then you try to find a game object that’s named “CanBePulledByGrav”, tags and game object names are two separate things. A tag is a string you attach (“tag”) onto game objects, this can be a one to many relationship (multiple game objects with different names can have the same tag). A game object’s name is simply the name you see of the game object in the hierarchy.

If you want to check the name of the game object that entered the trigger area, you can check the name like this:

    void OnTriggerEnter(Collider other)
    {
        Debug.Log("Object entered the trigger");
        if (other.gameObject.name == "CanBePulledByGrav")
        {
            // This call would no longer be needed since we know the game object is named "CanBePulledByGrav"
            //GameObject obj = GameObject.Find("CanBePulledByGrav");
            transform.LookAt(other.transform);
            Debug.Log("If is working.");
        }
    }

@Magso Yes i’m trying to get the transform of the object entering the trigger.