Akzwar
1
Suddenly problem with lambda in foreach statement.
Current.Nodes.ForEach( n => Debug.Log( n.name ) );// Outputs node0, node1
foreach( Node node in Current.Nodes )
{
Node local_node = node;
Button button = Instantiate( Resources.Load<Button>( "Prefabs/Button" ) );
button.transform.SetParent( content );
button.GetComponentInChildren<Text>().text = node.name;
button.onClick.AddListener( () =>
{
Debug.Log( local_node.name );
} );
}
Clicking on buttons always prints node1
This is likely an issue with a closure around the local_node variable. Try copying the name of the node into a temporary local variable:
Current.Nodes.ForEach( n => Debug.Log( n.name ) );// Outputs node0, node1
foreach( Node node in Current.Nodes )
{
string nodeName = node.name;
Button button = Instantiate( Resources.Load<Button>( "Prefabs/Button" ) );
button.transform.SetParent( content );
button.GetComponentInChildren<Text>().text = nodeName ;
button.onClick.AddListener( () =>
{
Debug.Log( nodeName );
} );
}
Akzwar
3
Everything because of this code is in couroutine. Here is the answer
http://answers.unity3d.com/answers/974195/view.html