LoginSignup
0

More than 5 years have passed since last update.

for文内でHandler登録

Last updated at Posted at 2016-01-15

for文内でclickHandlerを登録するには

example.cs
for (int i = 0; i < 10; i++) {
    _buttonList[i].OnClickHandler += () =>{
    Debug.Log(i.ToString ());
    }
}

としたいが、この場合どのボタンを押しても
9
が表示されてしまう。
これを解消するには

example.cs
for (int i = 0; i < 10; i++) {
  int tmpIndex = i;
    _buttonList[i].OnClickHandler += () =>{
    Debug.Log(tmpIndex.ToString ());
    }
}

一旦格納してあげるとOK

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0