Noobie: touch gameobject

Hi,

I am trying to implement a script to destroy a gameobject when it is touched. I have the code (as below) but am unable to get it working - nothing seems to happen when i touch the gameobject. I looked through the earlier related posts in the forum and am not able to spot whats missing in my script. I have attached this script to the gameobject. Any pointers would be of great help - am I missing something obvious? Thanks very much.

function Update ()
{
var hit:RaycastHit;

for ( var evt : iPhoneTouch in iPhoneInput.touches )
{

if( evt.phase == iPhoneTouchPhase.Began )
{
var ray = Camera.main.ScreenPointToRay(evt.position);

if(Physics.Raycast(ray, hit, Mathf.Infinity) hit.collider.tag == (“enemy”))
{
animation.Play(“die”);
Destroy( hit.collider.gameObject, 5 );
}
}
}
}

I stuck your code in a test project I had open and it worked fine. I removed the animation line though because I was messing with primitives that don’t have animation. So the animation may be part of your problem. Maybe the Destroy call can’t be made on a GameObject while it’s playing an animation? I dunno.

Try remarking out you animation call and see what happens.

Hi

Thanks, I tried it on a cube gameobject (removed the aimation) and I could not get it to work either. Do I have to set up a few other components etc as well?

Thanks

Odd. I’ve attached the test project I used, maybe that will help. I used Delete with no time argument. I figure you’re die animaiton was five seconds and that’s why the original code had a 5 arg. I had no animation, and didn’t like waiting, so I took it out.

HTH

201437–7428–$timedist_178.zip (470 KB)

Indeed it is quite odd. Thanks very much - I will test it and get back to you.

Bi

One thing I just thought of - I have the script on the main camera not the object to be deleted. That may be part of your issue.