I have a List playerList with a method GoSomething() on the component PlayerScript. I know I can do:
foreach(Player player in playerList) {
player.GetComponent<PlayerScript>().DoSomething();
}
But this results in quite a lot of foreach loops when working with lists. So is there a one-liner for calling a method for all objects in a list? Something like:
playerList.GetComponent<PlayerScript>().DoSomething();
You can use Lists
's ForEach
method:
playerList.ForEach(player => player.GetComponent<PlayerScript>().DoSomething())
Here is more
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.