- Home /
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++;
}
Answer by roamcel · Aug 17, 2011 at 05:13 AM
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.
Just curious, what would the more elegant solutions be? I came across this while googling my issue:
Every time a trigger is entered, I send a message to my game$$anonymous$$anager script to update the score:
using UnityEngine;
using System.Collections;
public class TunaCan : $$anonymous$$onoBehaviour {
public GameObject game$$anonymous$$gr;
public int pointValue;
public GameObject pickupAudio;
void OnTriggerEnter() {
Instantiate(pickupAudio, transform.position, transform.rotation);
game$$anonymous$$gr.Send$$anonymous$$essage("UpdateScore", pointValue);
iTween.ScaleBy(gameObject, iTween.Hash(
"amount", Vector3.one * 2.0f,
"time", 0.15f,
"easeType","easeOutCubic",
"oncomplete", "DestroySelf",
"oncompletetarget", gameObject));
}
void DestroySelf() {
Destroy(gameObject);
}
}
Then in my game$$anonymous$$anager script:
using UnityEngine;
using System.Collections;
public class Game$$anonymous$$anager : $$anonymous$$onoBehaviour {
public UILabel scoreText;
public static int score;
// Use this for initialization
void Start () {
score = 0;
scoreText.text = score.ToString();
}
void UpdateScore(int pointValue) {
score += pointValue;
scoreText.text = score.ToString();
}
}
Once in a while, if I hit two triggers close enough together, I get a higher score total than I should. I used the flag method you mentioned above, and that seemed to fix it. Just wondering if having that approach is somehow not optimal?
Thanks!
Answer by scottfredeman · Aug 17, 2011 at 02:24 PM
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++;
}
Your answer
Follow this Question
Related Questions
Trying to get score counter... 2 Answers
Scoring (i'm stuck) 3 Answers
Make a custom score counter in unity with c# 1 Answer
how to keep track of bricks broken 1 Answer
On Trigger Enter just triggers once for same object 1 Answer