Score Count error

Why is it that the score keeps on increasing even if I only want to include a certain object? I can’t seem to find the problem.

https://playcanvas.com/editor/scene/1135821

Can you point users where what code to look at and how to reproduce the error when playing the game please?

So here is the code in the first (1-29) :

var CharacterMovementControls = pc.createScript('characterMovementControls');

//for movement function
var movementSpeed = 120;

// for score
var counter = 0;

var score = 0;

// initialize code called once per entity
CharacterMovementControls.prototype.initialize = function() {
  
     this.entity.collision.on('collisionstart', this.onCollisionStart, this);

    //scoredisplay 
        var score = this.app.root.findByName("ScoreDisplay");
    
        score.element.text = counter.toString();

};

This is the code for the last 64-87;

CharacterMovementControls.prototype.onCollisionStart = function(result) {


    if (result.other.name == "meat") { // result.other refers to the entity that is being collided with 
        
        
        result.other.destroy(); //basically destroys the coin
        
        this.entity.sound.play('ting');
    }
    
    
     if (result.other.name == "loot") { // result.other refers to the entity that is being collided with 
        result.other.destroy(); //basically destroys the coin
         this.entity.sound.play('ting');
    }
    
     //codes for the score
    counter += 1;
    var score = this.app.root.findByName ("ScoreDisplay");
    score.element.text = counter.toString();


}; 

(I did not include the middle part of the codes since they are all keyboard controls but if you want to look, here is the link: https://playcanvas.com/editor/code/787509?tabs=46340663)
The problem is that although there is a score included when colliding with the “meat” ( where score should be got) whenever I collide with blocks or move a block the points/score keeps on increasing.

1 Like

Looks like this is for MMINTDS lol
try using this for your score? i think you shouldn’t write the score code outside of the collision event

    if (result.other.name == "meat"){
        result.other.destroy();
        this.entity.sound.play('ting');
        
        counter += 1;
        
        var score = this.app.root.findByName("ScoreDisplay");
        score.element.text = counter.toString();
        
    }
2 Likes

yes it is for MMINTDS lol. Thank you, I’ll try this

(edited:) It worked!! Thank you very much!

no prob!

good luck on your finals! :slight_smile:

1 Like