Help with Lists and index.

Im making a game for a project and my current code randomly generates coins in different locations, and im trying to make it so whenever my character collides with a coin, only that specific coin gets deleted. I have a method that almost works perfectly but there is an issue with one coin. This one coin will not follow the code and delete itself from my screen but it will add to the score as well as lower the List.Count by 1. I checked this coins index and found out that its index is always [23], which i can tell is important but i can't figure out how to solve it.
No description
No description
No description
18 Replies
WEIRD FLEX
WEIRD FLEX5mo ago
you are modifying the CoinSatchel collection while you are enumerating it, which is not a great thing to do
BricksAreTasty
BricksAreTasty5mo ago
who would i go about it then? how*
Jimmacle
Jimmacle5mo ago
looks like it's being done correctly though, if you have to do it iterating backwards is the solution
WEIRD FLEX
WEIRD FLEX5mo ago
yes, i can't see why it wouldn't work how many coins are there, 27? ah no 24 lol
BricksAreTasty
BricksAreTasty5mo ago
24 haha yeah'
WEIRD FLEX
WEIRD FLEX5mo ago
i am not sure i understand the problem because you say
delete itself from my screen but it will add to the score as well as lower the List.Count by 1
which is what i see in the code
if (collision) {
CoinSatchel.Remove; // remove, decrease list.count
score++; // increase score
}
if (collision) {
CoinSatchel.Remove; // remove, decrease list.count
score++; // increase score
}
are you saying it does that without a collision?
BricksAreTasty
BricksAreTasty5mo ago
ah yeah, the score is not the issue, its that when i collide with the other coins, the CoinSatchel.Count i put on the side never changes, the number stays at 24. But when i collide with this coin specifically, the number goes down by 1 to 23
BricksAreTasty
BricksAreTasty5mo ago
the CoinSatchel.Count never changes even when all the other coins are removed like in the first pic, but the moment i collide with the 'special' coin it changes
No description
No description
BricksAreTasty
BricksAreTasty5mo ago
if it helps, this is the part of the function that randomizes the positions and adds it to the coinsatchel (the n is just 24)
No description
WEIRD FLEX
WEIRD FLEX5mo ago
you can paste the $code instead of a screenshot, it makes it a little easier for everyone
MODiX
MODiX5mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
WEIRD FLEX
WEIRD FLEX5mo ago
is the last coin game ending? because again on every collision CoinSatchel is being modified so either there are two arrays with the coins, or you are resetting it every frame but when the game ends the game loop is not run and it prints the last Count value
BricksAreTasty
BricksAreTasty5mo ago
no i made the criteria for the score to have reached 24 instead, and then to collide with the box in the center wait i'll try and send the whole code thing or whatever $codegif
BricksAreTasty
BricksAreTasty5mo ago
https://paste.mod.gg/jqxtdyddidox/0 i think this link shows u the whole code
BlazeBin - jqxtdyddidox
A tool for sharing your source code with the world!
WEIRD FLEX
WEIRD FLEX5mo ago
if i can i'll give it a look but i don't really use unity
BricksAreTasty
BricksAreTasty5mo ago
Oh this isn’t on unity, it’s just raylib from vs code
WEIRD FLEX
WEIRD FLEX5mo ago
ah