# Score question/problem

**URL:** https://forum.code.org/t/score-question-problem/21313
**Category:** Unit and Lesson Discussion
**Tags:** csd-unit-3, csd-unit-3-lesson-22
**Created:** [October 24, 2018, 11:38pm UTC](https://forum.code.org/t/score-question-problem/21313 "2018-10-24T23:38:04Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![sarah.dudley](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@sarah.dudley](https://forum.code.org/u/sarah.dudley)
#### Post date: [October 24, 2018, 11:38pm UTC](https://forum.code.org/t/score-question-problem/21313/1 "2018-10-24T23:38:05Z")

</div>

Here is a copy of the code. Everything on the game seems to be working except it will not add the score when the apples touch the dog. We cannot figure out what is wrong.

// Create your variables here  
var cactus = createSprite(320, 320);  
cactus.setAnimation(“cactus\_1”);  
cactus.scale = 0.8;  
var dog = createSprite(55, 350);  
dog.setAnimation(“dog”);  
dog.scale = 0.2;  
var apple = createSprite(0, 200);  
apple.setAnimation(“apple\_1”);  
apple.scale = 0.4;  
apple.velocityY = 5;  
var score = 0;

// Create your sprites here

function draw() {  
myFunction();  
collision();  
showapple();  
if (score \< 11) {  
background1();  
} else {  
background2();  
}  
drawSprites();  
if (keyDown(“right”)) {  
dog.x = dog.x + 5;  
}  
if (keyDown(“left”)) {  
dog.x = dog.x - 5;  
}  
showscore();

}  
function showscore() {  
fill(“black”);  
textSize(20);  
text(“score:”, 5, 5, 20, 20);  
text(score, 70, 5, 20, 20);  
}  
function myFunction() {  
if (dog.isTouching(apple)) {  
apple.x = randomNumber(0, 400);  
apple.y = 0;  
}  
}  
function background1() {  
background(“lightblue”);  
fill(“green”);  
rect(0, 370, 400, 30);

}  
function background2() {  
background(“red”);  
fill(“green”);  
rect(0, 370, 400, 30);

}  
function showapple() {  
if (apple.y \> 410) {  
apple.x = randomNumber(0, 400);  
apple.y = 0;  
}  
}  
function collision() {  
if (apple.isTouching(dog)) {  
score = score + 1;  
}  
}

---

<div class="post-metadata">

### Author: ![karenma](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/karenma/32/4924_2.png) [@karenma](https://forum.code.org/u/karenma)
#### Post date: [October 26, 2018, 2:08am UTC](https://forum.code.org/t/score-question-problem/21313/2 "2018-10-26T02:08:01Z")

</div>

They may want to try something like this:  
function playerTouchesObject(){  
if(player.isTouching(snakeToken)){  
setSnake();  
score = score + 4;

The full code used for a game that increases the score when two sprites touch is found on lesson 22 bubble 11 under the example solution tab in the teacher panel.
