GUITexture Button Help, Android touch

I have a GUITexture button im trying to get working from a mobile app im working on for android. Im trying to be able to touch the button have it display “TOUCHED” in the Debug.Log. The Problem is that when i touch the GUITexture button, the only place that it registers the touch is in the top right corner of the GUITexture Button. This is the code i have so far:

private var gui : GUITexture;

private var defaultRect : Rect;

private var guiTouchOffset : Vector2;



function Start(){

gui = GetComponent( GUITexture );



// get where the gui texture was originally placed

defaultRect = gui.pixelInset;



// get our offset for center instead of corner

guiTouchOffset.x = defaultRect.width;

guiTouchOffset.y = defaultRect.height;

}





function Update(){



var count = Input.touchCount;



// account for the offset in our calculations



for (var i: int = 0; i < count; i++){



var touch : Touch = Input.GetTouch(i);



var guiTouchPos : Vector2 = touch.position - guiTouchOffset;



if (gui.HitTest( touch.position )){

Debug.Log("Touched");

gui.pixelInset.x = guiTouchPos.x;

gui.pixelInset.y = guiTouchPos.y;

}

}

}

any help Is greatly appreciated. This thing has been troubling me for the longest time now. Thanks

Never mind. I answered my own question. Here is two ways to do it:

  • function Update()

    {

    for (var touch : Touch in
    Input.touches)

    {

    if (guiTexture.HitTest
    (touch.position))

    {

    // we are now in the guitexture
    
    Debug.Log("Touch"); 
    

    }

    }

    }


private var gui : GUITexture;


function Start(){
gui = GetComponent( GUITexture );
var newInset : Rect = new Rect(0,350,134,40);
guiTexture.pixelInset = newInset;
}

function Update(){

if(Input.touchCount > 0){

for(var i : int = 0; i < Input.touchCount; i++){

var touch : Touch = Input.GetTouch(i);

if(touch.phase == TouchPhase.Began &&

guiTexture.HitTest(touch.position)){

Debug.Log("logged");
}
}
}
}


This is a cleaner and more understandable answer!

http://answers.unity3d.com/questions/422187/android-guitexture-touch.html