Compare enum name to a tag

Hello,

I am trying to compare an enum type name to a tag in a check inside my code.

How can I do this successfully?

Here is my code:

public enum InteractableType { Enemy, ItemPickup }

public class Interactable : MonoBehaviour {

  public InteractableType interactableType;

}

public class MouseScript : MonoBehaviour {

Interactable focus;

  void Update ()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if(Physics.Raycast(ray, out hit, 100))
        {
            // Check if we hit an interactable
            focus = hit.collider.GetComponent<Interactable>();
            if(focus != null)
            {
                for (int i = 0; i < cursorList.Count; i++)
                {
                    if (focus.interactableType.ToString == cursorList[i].tag)
                    {
                        SetCursorTexture(cursorList[i].cursorTexture);
                        return;
                    }
                }
            }   
        }
        // If the raycast does not return anything, we set the default cursor
        SetCursorTexture(cursorList[0].cursorTexture);
    }
}

Thank you!

Did you get any compiler errors?

1 Like

The simple thing you can do is to put a debug.log so you can see what the two values are.

for (int i = 0; i < cursorList.Count; i++)
                {
                    Debug.Log(focus.interactableType.ToString() + " : " + cursorList[i].tag);
                    if (focus.interactableType.ToString() == cursorList[i].tag)
                    {
                        SetCursorTexture(cursorList[i].cursorTexture);
                        return;
                    }
                }

You are missing () on the ToString

1 Like

You ruined my post, @johne5 ! (just kidding, eh.)

1 Like

Thanks @johne5 it was indeed missing the ‘()’, now it works perfectly.
And thank you also @methos5k .