Search Unity

Check on what object I clicked

Discussion in 'Scripting' started by malnar1996, Jan 11, 2011.

  1. malnar1996

    malnar1996

    Joined:
    Oct 31, 2010
    Posts:
    31
    Hello.
    I was wondering how can I check what's the name of the object i clicked. I almost finished my game and I want to add a new feature. When the user slicks on a place in the map, a new soldier gets created there. I was wondering how can i check on what the user clicked (i want to restrict user from creating a new soldier on a place with water.)

    How can I check this. Thank you.
     
  2. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    The tag system works pretty good for this feature. Just add it to your conditional statements.

    function OnMouseDown() {
    if(gameObject.tag == "land") {
    //Do what i want.
    }
    }

    Something like this can help control things.
     
  3. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    yes, use tags on the objects in the scene and based on those you can decide if you can do an action
     
  4. malnar1996

    malnar1996

    Joined:
    Oct 31, 2010
    Posts:
    31
    Thank you for your answers but I already know about OnMouseDown function. I was wondering if I could do this from another object (not terrain)
     
  5. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326