Search Unity

How to listen to touch event on single game object?

Discussion in 'iOS and tvOS' started by vvloo, Jan 26, 2010.

  1. vvloo

    vvloo

    Joined:
    Jan 26, 2010
    Posts:
    25
    Hi everyone, is there any way to listen to touch event on single game object while you are developing iPhone with Unity for iPhone?

    I attached same script, which is written in JS, to ten different game objects. But when I touch anywhere on the screen, all these ten objects response to the touch event. What I want is when I touch each of these objects, only the one I touched would response to the touch event.

    Can anybody help?

    Many thanks.

    Carol
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    touch events are not object specific, just screen specific.

    If you need to know what object you touch, you need to do a raycast at the touch position into the ray direction of the camera.
     
  3. vvloo

    vvloo

    Joined:
    Jan 26, 2010
    Posts:
    25
    I tried, but the problem is I am using guitexture which does not work for raycast.
    Is there any other way to solve this problem??

    Many thanks.
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Can you post the script you are using? It should be possible to arrange things so that each instance responds separately, but there's no way to tell what is going wrong without seeing the script.
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Thats actually easy: just add a box collider and set its size appropriately
     
  6. vvloo

    vvloo

    Joined:
    Jan 26, 2010
    Posts:
    25
    I added a box collider to the gui texture which would be hit, but it still did not work.

    here is my code.
    in code attached to the background (guitexture):

    function LevelControl()
    {
    var hit: RaycastHit;
    if (iPhoneInput.touchCount > 0 iPhoneInput.GetTouch(0).phase == iPhoneTouchPhase.Ended)
    {
    var ray: Ray = camera.main.ScreenPointToRay(iPhoneInput.GetTouch(0).position);
    print("ray:" + ray);

    if(Physics.Raycast(ray,hit))
    {
    print("hit");
    }
    }
    }


    in the code attached to the guitexture which will be hit:

    function OnTriggerEnter(other: Collider)
    {
    guiTexture.texture = clicked;
    print(other.name);
    }


    I can see the information of ray but not the "hit" and other.name.
     
  7. voidmaystorm

    voidmaystorm

    Joined:
    Jun 11, 2009
    Posts:
    89
    hi,


    just try:


    Code (csharp):
    1. // assign the guitexture in the editor
    2. var Slot1 : GUITexture;
    3.  
    4. if ( Slot1.HitTest(theTouch.position) ) {
    5.  
    6.  // do sth.;
    7.  
    8. }

    you dont need a collider or a raycast to check if a guitexture was touched.

    greetings, thomas
     
  8. vvloo

    vvloo

    Joined:
    Jan 26, 2010
    Posts:
    25
    It's working!

    Million thanks.
     
  9. mrfatameen2

    mrfatameen2

    Joined:
    Jan 13, 2019
    Posts:
    1
    you can help me how I can execute more collider on one object
    Each collider has script