Hi i am trying to see if one list contains any item from another list by this code
for (int j = 0; j < spawnedCoins.Count; j++) {
for (int i = 0; i < connectedTiles.Count; i++) {
if (spawnedCoins[j].transform.position.x == connectedTiles[i].x && spawnedCoins[j].transform.position.y == connectedTiles[i].y)
{
coinsCollected++;
Destroy (spawnedCoins [j]);
spawnedCoins.RemoveAt (j);
}
}
}
However Sometimes it finds a match and destroys the object but then sometimes it does not find a match where it is clearly a match. I have confirmed many times that the x and y values of both elements are the same but still NO match is found and it does not go inside if statement. And thus coinsCollected score is not added and coins are not deleted in game.
I have also used contains like this but with same problems
for (int j = 0; j < spawnedCoins.Count; j++) {
if (connectedTiles.Contains (spawnedCoins[j].transform.position)) {
coinsCollected++;
Destroy (spawnedCoins [j]);
spawnedCoins.RemoveAt (j);
}
}
Can anyone tell me why this is happening?
Thanks
↧