Search Unity

Compare enum name to a tag

Discussion in 'Scripting' started by henmachuca, Feb 8, 2018.

  1. henmachuca

    henmachuca

    Joined:
    Oct 14, 2016
    Posts:
    95
    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:
    Code (CSharp):
    1. public enum InteractableType { Enemy, ItemPickup }
    2.  
    3. public class Interactable : MonoBehaviour {
    4.  
    5.   public InteractableType interactableType;
    6.  
    7. }
    8.  
    9. public class MouseScript : MonoBehaviour {
    10.  
    11. Interactable focus;
    12.  
    13.   void Update ()
    14.     {
    15.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    16.         RaycastHit hit;
    17.  
    18.         if(Physics.Raycast(ray, out hit, 100))
    19.         {
    20.             // Check if we hit an interactable
    21.             focus = hit.collider.GetComponent<Interactable>();
    22.             if(focus != null)
    23.             {
    24.                 for (int i = 0; i < cursorList.Count; i++)
    25.                 {
    26.                     if (focus.interactableType.ToString == cursorList[i].tag)
    27.                     {
    28.                         SetCursorTexture(cursorList[i].cursorTexture);
    29.                         return;
    30.                     }
    31.                 }
    32.             }  
    33.         }
    34.         // If the raycast does not return anything, we set the default cursor
    35.         SetCursorTexture(cursorList[0].cursorTexture);
    36.     }
    37. }
    38.  
    39.  
    Thank you!
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
    Did you get any compiler errors?
     
    henmachuca likes this.
  3. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,091
    The simple thing you can do is to put a debug.log so you can see what the two values are.
    Code (CSharp):
    1. for (int i = 0; i < cursorList.Count; i++)
    2.                 {
    3.                     Debug.Log(focus.interactableType.ToString() + " : " + cursorList[i].tag);
    4.                     if (focus.interactableType.ToString() == cursorList[i].tag)
    5.                     {
    6.                         SetCursorTexture(cursorList[i].cursorTexture);
    7.                         return;
    8.                     }
    9.                 }
    You are missing () on the ToString
     
    henmachuca likes this.
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
    You ruined my post, @johne5 ! (just kidding, eh.)
     
    henmachuca likes this.
  5. henmachuca

    henmachuca

    Joined:
    Oct 14, 2016
    Posts:
    95
    Thanks @johne5 it was indeed missing the '()', now it works perfectly.
    And thank you also @methos5k.
     
unityunity