How to clear List elements in a foreach statement
Enemy detection method
Add the enemy object detected by the collider set as the child object of Player to the colliderList,
Processing for those elements is performed every frame in the Update function. (Specify only the Object you want to detect in the Layer settings)
Points to improve
I want to improve the phenomenon that the HP of the opponent’s object becomes 0 or less and the death animation is performed, but the attack continues until the death animation ends and Destroy is done (corpse kick).
But with a simple idea, I get an error
//I want to remove enemies with HP 0 or less from the collider List
foreach(GameObject list in colliderList)
{
if(list.GetComponent<Enemycontroller>().currenthp <= 0)
{
colliderList.Remove(list);
}
}
solution
I created a new RemoveList to store unnecessary elements.
//I want to remove enemies with HP 0 or less from the collider List
foreach(GameObject list in colliderList)
{
if(list.GetComponent<Enemycontroller>().currenthp <= 0)
{
//Store unnecessary elements
RemoveList.Add(list);
}
}
//Removes objects stored in RemoveList from colliderList
foreach (GameObject list in RemoveList)
{
colliderList.Remove(list);
}
//Remove all elements in RemoveList
RemoveElementList.Clear();
After a lot of research, I found a lambda method, but this method was the easiest to understand.
- I didn’t know how to put out backticks (`← this)
Solved with Shift + @ ^^;