[SOLVED] Need help with triggering score increase once per object only

Hi, I am currently making an educational game, and the gameplay is that the player will need to collide with certain objects to increase score (10 in total) and access the information that will display once collided with. BUT the score must only be increased once per object while still being able to display the information to avoid spam colliding with one object only. How do I make the score increase trigger only once per object? Thank you so much!

Here is the link to my editor: PlayCanvas | HTML5 Game Engine

My current workaround does not increase my score anymore after colliding with one object (score remains at 1) . Here is the code that I am working with.

Fpmove.prototype.OnCollisionStart = function(result) {

// FOR OBJ SCORE

if (result.other.name == "ArimaongaObj"){

    score = this.app.root.findByName("ScoreDisplay");

    //this.entity.sound.play('SFXDAMAGE');

    this.app.root.findByName('Crosshair').enabled = false;

    this.app.root.findByName('infoArimaonga').enabled = true;

    score_counter++;

    score.element.text = score_counter.toString();

    if (score_counter++){

        score_counter = false;

    }

}

if (result.other.name == "DwarfObj"){

    score = this.app.root.findByName("ScoreDisplay");

    //this.entity.sound.play('SFXDAMAGE');

    this.app.root.findByName('Crosshair').enabled = false;

    this.app.root.findByName('infoDwarf').enabled = true;

   

    score_counter++;

    score.element.text = score_counter.toString();

    if (score_counter++){

        score_counter = false;

    }

}
};

Hi @angela_02 and welcome!

There are a couple of ways to do this. You can give for example the objects a score tag and remove this tag after the first collision. This way you can add the score only once.

if (result.other.name == "ArimaongaObj"){
    score = this.app.root.findByName("ScoreDisplay");
    this.app.root.findByName('Crosshair').enabled = false;
    this.app.root.findByName('infoArimaonga').enabled = true;

    if (result.other.tags.has('score')) {
        result.other.tags.remove('score');
        score_counter++;
        score.element.text = score_counter.toString();
    }
}

This is probably because you change the number into a boolean with score_counter = false; and score_counter++; doesn’t work on a boolean.

1 Like

Thank you so much for this! However, when I tried the code, it showed me this error. I don’t understand why?

[Update: I caught the typo and fixed it!]

Thanks! I also fixed it in my example. :smiley:

1 Like

Thank you so much for your help, it works now!

1 Like