Search Unity

How to display a message when a gameobject is touched

Discussion in 'Scripting' started by rasarahul, Jan 20, 2011.

  1. rasarahul

    rasarahul

    Joined:
    Jan 19, 2011
    Posts:
    12
    Hello All,
    Can anyone please help me with this??? How to display a message when a gameobject is touched?? I was stuck up with this
     
  2. mattcscz

    mattcscz

    Joined:
    Mar 7, 2010
    Posts:
    411
    When touched? do you mean with a mouse? or with collision?

    Click (attach this script to the game object):

    Code (csharp):
    1.  
    2. var OverO = false;
    3. function OnMouseOver () {
    4. OverO = true;
    5. if(Input.GetMouseButtonDown(0)  OverO){
    6. print("hi");
    7.     }
    8. }
    9. function OnMouseExit (){
    10. OverO = false;
    11. }
    12.  
    or with collision, easyiest way would be to use a trigger (attach this to a trigger(the trigger should be about the size of a collision box around the object)):

    Code (csharp):
    1.  
    2. function OnTriggerEnter () {
    3. print("hi");
    4. }
    5.  
    but ofcourse is you want it to happen with a collision so that you can't walk through the object i would suggest adding a collision box and a trigger thats slightly larger. Alternativly if you search the forum for collisions then you can do somthing with OnCollide
     
  3. rasarahul

    rasarahul

    Joined:
    Jan 19, 2011
    Posts:
    12
    Thank u mattcscz im working with android so could u help me regarding that