OnMouseDown on Iphone

I want to be able to select an object in my scene by clicking it. The following simple function works to select an object in the scene when running in regular Unity.

var isSelected = false;

function OnMouseDown() {
   isSelected = true;
}

When I try use this function in Iphone Unity, It does not work and there are no errors. Any ideas would be much appreciated.

The iPhone doesn’t use Mouse events, use iPhoneInput instead.

Here is a good post that describes it and has a code code example…
http://forum.unity3d.com/viewtopic.php?t=15264&highlight=iphoneinput

Yes I saw that post. However what this doesn’t address is how to test whether or not you actually touched a specific object. This is what the OnMouseDown behavior does, its test if you have clicked an object. I was assuming since other mouse based scripts have mapped over by default to the touch interface, that an OnMouseDown function would just work. This also makes it easier to test if the mouse based functions are also in there.

So…what I am looking to do is touch(or Click) an object in the scene and show some info about the object. Any ideas there? This seems like it should be very straight forward.

You need to cast a ray from the camera position into the scene. You can use ScreenPointToRay to to get a Ray with the touch position: Unity - Scripting API: Input.mousePosition

Input.GetMouseButtonDown(0) is emulated by touches (as is Input.mousePosition).

You could do something like:

function Update()
{
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	var hit:RaycastHit;

	if(Physics.Raycast(ray, hit, 100))
	{
		if(hit.collider.name == "SomeObject")
			DoWhatever();

		// or you could event send the message yourself
		hit.collider.SendMessage("OnMouseDown", SendMessageOptions.DontRequireReceiver);
	}
}

Thank, This didn’t quite work however. I had to make the following changes, but have it mostly working now.

var explosion : Transform;
var target : Transform;

function Update () {
if (Input.GetButtonDown ("Fire1")) {
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	var hit:RaycastHit; 
		if (Physics.Raycast (ray, hit, 100)) {
			if(hit.collider.name == "Target") 
				Instantiate (explosion, target.position, target.rotation);
		}
	}
}

One problem I am still having though is how to expose a variable for the target. This script seems to only work if I explicitly hard code the object name here: if(hit.collider.name == "Target")

any idea on OnMouseButtonUp? Or any other way to determine when the user has let go of a GUI button?

@jedda

The new example I posted on the resources page, “iPhone-Match”, deals with some of this stuff.