Destroy on touch for iphone

When I'm trying to destroy an object when its clicked I simply use on mouse down destroy game object but When I port to iphone that 1. doesn't work cause of mouse commands but then 2. I fix it to use touch and instead of when I click the object it destroys it, it actually will simply destroy the object no matter where I click (touch).

Here is the code but the desired effect is when I touch that object it destroys itself...

#pragma strict
var objectToKill : GameObject;

function Update(){

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

        if (Physics.Raycast (ray, hit, 100)) {

        }
        if (Input.GetMouseButtonDown(0)){
            if (hit.collider.name == objectToKill)
            {
                Destroy(gameObject);
            }
        }
}

Also, Input.GetMouseButtonDown does convert to iphone fine other scrips I have use it. but in this case I did change it to iPhoneInput.touchCount == 1 which also works.

I must know, did you place this code dirrectly on the target of destruction or on the camera?

1 Answer

1

I figured it out. Here is what I ended up doing if any one else is in a similar situation. This works on iphone. Probably don't need to specify tag. You could probably just use gameObject.

#pragma strict

function Update () {

    var hit: RaycastHit;
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) 
    {
        if (Physics.Raycast (ray, hit, 100)) 
        {
            if(hit.collider.gameObject.tag == "tagOfYourObect")
            {
                Destroy(hit.collider.gameObject);
            }
        }
    }
}