- Home /
Why does my counter keep going up after pickup is destroyed?
Hi,
I have this code which adds 1 to the OBJECTNUMBER every time someone picks up an object. Except sometimes, it adds 2 or 3 to OBJECTNUMBER. I'm not sure why this is as the object is destroyed as soon as my player comes into contact with it. I've followed a tutorial and I've got the same code as that did and it seemed to work fine on the video. Any suggestions?
else if(hit.gameObject.tag == "Helmet1") {
soundObj.audio.PlayOneShot(pickup);
Destroy(hit.gameObject, 0.01);
OBJECTNUMBER += 1;
g_Helmet.animation.Play("g_Inv_HelmetAnimation2");
print("you now have" + OBJECTNUMBER);
}
Thanks
Answer by duck · May 03, 2010 at 01:50 PM
You have a delay value specified in your Destroy function, of 0.01. This means that your object will be destroyed 0.01 seconds after the object is collected. If your game is running at a fast enough framerate, this can be enough time for another frame or two to elapse before the object is finally destroyed.
You should remove that value completely, so the line looks like this:
Destroy(hit.gameObject);
Thankyou, can't believe I didn't spot this. That'll $$anonymous$$ch me for following the tutorials to the letter :)