Ask coding questions

← Back to all posts
How can I make a code that automatically changes when player collects gold
ichbindede

I'm making a game and in this game there is a gold counter and I want it to add the gold number when the player collects golds.

Voters
ichbindede
Comments
hotnewtop
Coder100

you can first assign it something:

and then

ichbindede

thanks. but how can I add the gold number when player overlaps golds. I wrote this:

Coder100

it doesn't because of shadowing.
As you can see, you passed in the gold as an argument of the object of that gold coin. So that means you have to rename the gold score to something else, like maybe goldScore @ichbindede

ichbindede

still not working:

player.overlaps("gold", (gold) => {
destroy(gold);
goldScore.text = Score: ${goldScore} + 1;
});`

still not working
@Coder100

Coder100

you are still not understanding me

@ichbindede

ichbindede

ok thanks. it half worked. the gold is resetting every level and i can't collect multiple golds. it can be only 1. how can i fix that @Coder100

Coder100

you forgot to increment goldScore right now you have:

you should have

do this for every single one
@ichbindede

ichbindede

yeah now it can collect multiple golds but now it's resetting the gold when it goes to another level @Coder100

Coder100

you have 2 pieces of the same code, so that is why.

remove this:

@ichbindede

ichbindede

no that wasn't the problem. i deleted that but it's keep resetting the gold number. @Coder100

Coder100

because you went to a different scene so that means the score got reset. @ichbindede

Coder100
ichbindede

so is there a fix to this. @Coder100

Coder100

did you even read the link @ichbindede