Game Object On Touch Event on Android-iPhone 2D

Hi everyone,

I’m new at unity and trying to figure out how to detect touches on the gameObject. First of all I created a sprite sheet and put it into the material. For the game object I am using this material and making sprite animation with it.

Thus, basically I want to do something when someone touch the object on android phone.

-How can i detect the touches on the gameobject?

-I don’t know how collider works do i need it for detect touches?

-Generally i’m using javascrips for the scripting. Do i have to use C# scripts ?

-You can detect what was touched and send a message to the touched gameobject.

-You need colliders to detect touch. I simply use a box collider.

-You can use javascript.

I wrote this script:

var platform : RuntimePlatform = Application.platform;

function Update(){
	if(platform == RuntimePlatform.Android || platform == RuntimePlatform.IPhonePlayer){
		if(Input.touchCount > 0) {
			if(Input.GetTouch(0).phase == TouchPhase.Began){
	}else if(platform == RuntimePlatform.WindowsEditor){
		if(Input.GetMouseButtonDown(0)) {

function checkTouch(pos){
	var wp : Vector3 = Camera.main.ScreenToWorldPoint(pos);
	var touchPos : Vector2 = new Vector2(wp.x, wp.y);
	var hit = Physics2D.OverlapPoint(touchPos);

This works both in the editor (on windows at least) and on android+iphone.

It works by sending a message to the gameobject to run the function Clicked(), using a script on the gameobject that contains that function will make it run.
It also logs the name of the gameobject for debug purposes.

This only works for 2d colliders.

Also, you don’t need materials for displaying sprites.

Your game object needs a collider, and your Raycaster needs to be turned on, for touches to be detected. If you’re new to programming, you might buy a plug in to assist you with the coding.

Look at the last example on the documentation page:

All scripting in Unity can be performed with JS, c# or boo.

Hi guys, I am trying to do the same thing but here is not working that well,
I am able to drag the object by touching and dragging, but it won’t hold for long,
after a few milliseconds it falls Oo

I have tried many different approaches, same results, any help will be appreciated.

My code:

Touch touch = Input.GetTouch(i);
if (touch.phase == TouchPhase.Moved)
	Vector3 position = Camera.main.ScreenToWorldPoint(touch.position);

	Vector2 touchPos = new Vector2(position.x, position.y);
	RaycastHit2D hit = Physics2D.Raycast(touchPos,;

		GameObject tObject = GameObject.Find(;
		tObject.rigidbody2D.isKinematic = false;
		position.z = 0; 
		tObject.rigidbody2D.transform.position = position;
        cam.GetComponent<MouseCamera>().enabled = false;
        // enable the camera movement back            	
        cam.GetComponent<MouseCamera>().enabled = true;