You’re capturing the variable i
in your lambda expression. When that lambda expression is executed, it will use the “current” value of i
– which will always be 9. You want to capture a copy of the variable… which you can do be introducing a new variable in the loop:
for (int i = 0; i < teamButtons.Length; i++)
{
int index = i;
teamButtons[i].onClick.AddListener(() => SetCard(c[index]));
}
Related Contents:
- Serialize and Deserialize Json and Json Array in Unity
- How to detect click/touch events on UI and GameObjects
- What is the best way to save game state?
- How to pass data (and references) between scenes in Unity
- How to make the script wait/sleep in a simple way in unity
- Using new Unity VideoPlayer and VideoClip API to play video
- Using Resources Folder in Unity
- Unity Scripts edited in Visual studio don’t provide autocomplete
- Unity: Null while making new class instance
- Move GameObject over time
- Sending http requests in C# with Unity
- Access variables/functions from another Component
- Use Unity API from another Thread or call a function in the main Thread
- Accessing a variable from another script C# [duplicate]
- Unity game manager. Script works only one time
- Rotate GameObject over time
- How to access a variable from another script in another gameobject through GetComponent?
- Saving/loading data in Unity
- Simple event system in Unity
- “A namespace cannot directly contain members such as fields or methods” [closed]
- Unity – need to return value only after coroutine finishes
- In Unity (C#), why am I getting a NullReferenceException and how do I fix it? [duplicate]
- Simple socket server in Unity
- Can’t add script component because the script class cannot be found?
- Make a sphere with equidistant vertices
- Compare floats in Unity
- Unity how to make a Visual JoyStick in Unity
- In Unity, how can I pass values from one script to another?
- Play and wait for Animation/Animator to finish playing
- Using Layers and Bitmask with Raycast in Unity
- UnityWebRequest Embedding User + Password data for HTTP Basic Authentication not working on Android
- How do extension methods work?
- Unity singleton manager classes
- How to connect to database from Unity
- Objects in Scene dark after calling LoadScene/LoadLevel
- Build and load Assetbundles in Unity
- Create a coroutine to fade out different types of object
- How does StartCoroutine / yield return pattern really work in Unity?
- Fading in/out GameObject
- Enable/Disable VR from code
- Camera.main is null when performing raycast
- How to speed up the build and run process in unity for mobile devices iOS/Android [closed]
- Can’t change GameObject color via script?
- Declaring a new instance of a class in C#
- Embed Unity3D app inside WPF application
- Is Unityscript/Javascript discontinued?
- 3D relative angle sum calculation
- What is the best scripting language to embed in a C# desktop application? [closed]
- Get SSID of the wireless network I am connected to with C# .Net on Windows Vista
- What’s so bad about building XML with string concatenation?