Search Unity

Troubles with raycasting to find tags

Discussion in 'Scripting' started by Waffles, Oct 25, 2010.

  1. Waffles

    Waffles

    Joined:
    Oct 20, 2010
    Posts:
    6
    Hi all,
    I've been trying to make a crosshair that changes color when it hovers over objects with particular tags (i.e, "Friend" and "Enemy" tags). I had a look around, and saw you needed to raycast to find such things. Raycasting's unfamiliar territory for me, so I'm kinda flying blind here. This is the script I have:
    Code (csharp):
    1. var crosshairTexture : Texture2D;
    2. var crosshairRange = 200;
    3.  
    4. private var hit : RaycastHit;
    5. private var facingDirection = transform.TransformDirection(Vector3(0,0,1));
    6.  
    7. function Update ()
    8. {
    9.     if (Physics.Raycast(transform.position, facingDirection, hit, crosshairRange))
    10.     {
    11.         if(hit.collider.gameObject.tag != "Untagged")
    12.         {
    13.             if (hit.collider.gameObject.tag = "Enemy")
    14.             {
    15.                 crosshairTexture.color = Color.(1, 0, 0, 0.5);
    16.             }
    17.  
    18.             else if (hit.collider.gameObject.tag = "Friend")
    19.             {
    20.                 crosshairTexture.color = Color.(0, 1, 0, 0.5);
    21.             }
    22.  
    23.             else
    24.             {
    25.                 crosshairTexture.color = Color.(1, 1, 1, 0.5);
    26.             }
    27.         }
    28.     }
    29. }
    In line 13 - if (hit.collider.gameObject.tag = "Enemy") it comes up with the error "expecting ), found =." and also "Unexpected token Enemy." I've tried a few different versions of the hit.collider.gameObject.tag thing, none of which have worked. I know this is probably just a simple mistake (last time I forgot to capitalize a 'u') but I can't for the life of me figure it out.

    PS. Somebody told me that in the 'if' statements there needed to be == instead of just =, however when I tried that it came up with 27 errors...

    Any help would be appreciated!
     
  2. sangi93

    sangi93

    Joined:
    Aug 4, 2010
    Posts:
    77
    Try this:
    Code (csharp):
    1. var crosshairTexture : Texture2D;
    2. var crosshairRange = 200;
    3.  
    4. private var hit : RaycastHit;
    5. private var facingDirection = transform.TransformDirection(Vector3(0,0,1));
    6.  
    7. function Update ()
    8. {
    9.     if (Physics.Raycast(transform.position, facingDirection, hit, crosshairRange))
    10.     {
    11.         if(hit.collider.gameObject.CompareTag("Untagged"))
    12.         {
    13.             if (hit.collider.gameObject.CompareTag("Enemy"))
    14.             {
    15.                 crosshairTexture.color = Color(1, 0, 0, 0.5);
    16.             }
    17.  
    18.             else if (hit.collider.gameObject.CompareTag("Friend"))
    19.             {
    20.                 crosshairTexture.color = Color(0, 1, 0, 0.5);
    21.             }
    22.  
    23.             else
    24.             {
    25.                 crosshairTexture.color = Color(1, 1, 1, 0.5);
    26.             }
    27.         }
    28.     }
    29. }
     
  3. xomg

    xomg

    Joined:
    Sep 27, 2010
    Posts:
    330
    You do need to use ==, and not =. It's not like this is optional or something, they're entirely different.

    = is used to assign something to a variable. == is used to compare something.

    You also want to use CompareTag, I think.
     
  4. Waffles

    Waffles

    Joined:
    Oct 20, 2010
    Posts:
    6
    Excellent! Thanks sangi, that worked like a charm! Also, thanks for the clarification of the = symbols xomg, I couldn't tell the difference between the two before.

    Right.. now on to the next problem :p