Keeping Score registering more than one Point sometimes on Destroy

Hello,

Does anyone know of a way to force a score keeping script to register only one point when an object is destroyed, even if the object gets hit by multiple bullets at the same time?
Sometimes when I shoot my Object that has my Keepscore script it Registers more than once when that object is destroyed.
I am thinking that the object is not being destroyed fast enough sometimes because of a lower frame rate at that time and another bullet hits that same object and registers another point. I am not sure that’s what’s happening but it sounds logical.

Here is one of my scripts that has this problem:

Thank you in advance.
Scott

function OnCollisionEnter(collision : Collision) {

if (collision.gameObject.tag == "MISSILE")
Destroy (gameObject);
KEEPSCORE_SCRIPT.score++;

}

Most likely something like the example code at [the bottom of this MSDN page][1]. Note: I'm not sure how well this behaves in multi-monitor setups ^^. Another quick google revealed [this][2]. So GetSystemMetrics always return the resolution of the primary monitor. [1]: https://msdn.microsoft.com/en-us/library/55d3thsc.aspx [2]: https://msdn.microsoft.com/en-us/library/windows/desktop/dd162729(v=vs.85).aspx

2 Answers

2

Since it’s technically possible that the script gets triggered multiple times simultaneously (albeit I wouldn’t think it to be that easy as it happens in your case), what you possibly just need is a simple flag variable.

bool beenhitalready = false;

then your code would simply become

if (collision.gameObject.tag == "MISSILE" && beenhitalready == false)
  beenhitalready = true;
  KEEPSCORE_SCRIPT.score++;
  Destroy (gameObject);
}

If this works, you truly have a simple timing problem and there are other, more elegant solutions.

If it doesn’t work, your problem is not a timing one.

Thank you for your reply. Appears it may not be a timing problem. What else can be causing this to happen if the Timing is not doing it?
Thank you,
Scott

Here is my complete script:

var explosion : GameObject;

var beenhitalready : boolean = false;

function OnCollisionEnter(collision : Collision) {

var contact : ContactPoint = collision.contacts[0];

var thisExplosion : GameObject = Instantiate (explosion, contact.point + (contact.normal * 5.0) , Quaternion.identity);

if (collision.gameObject.tag == “MISSILE” && beenhitalready == false)

beenhitalready = true;

Destroy (thisExplosion, 4.0);

Destroy (gameObject);

KEEPSCORE_INSTANTIATE_AND_PLAY_AUDIO_ANY_LEVEL.score++;
}

sorry didn't mean take so long just say your replay.