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.
18 Replies
you are modifying the CoinSatchel collection while you are enumerating it, which is not a great thing to do
who would i go about it then?
how*
looks like it's being done correctly though, if you have to do it iterating backwards is the solution
yes, i can't see why it wouldn't work
how many coins are there, 27? ah no 24 lol
24
haha yeah'
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 1which is what i see in the code are you saying it does that without a collision?
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
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
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)
you can paste the $code instead of a screenshot, it makes it a little easier for everyone
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/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
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
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!
if i can i'll give it a look but i don't really use unity
Oh this isn’t on unity, it’s just raylib from vs code
ah