1

I have written this code to identify the number of cubes in the scene and create a button dynamically for each cube.and when a button is clicked the position of a cube will be displayed.but when I run this code only the final cubes position is shown.What am I doing Wrong here

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class buttons : MonoBehaviour {

    public GameObject MyButtonPrefab;
    public RectTransform ParentPanel;
    // Use this for initialization
    int bpos = 143;
    Button m;
    string a;
    void Start () {

        //Debug.Log(GameObject.FindGameObjectWithTag("Cube").transform.position);
        GameObject[] objects = GameObject.FindGameObjectsWithTag("Cube");

        int x = GameObject.FindGameObjectsWithTag("Cube").Length;
        Debug.Log(x);

        for (int i = 0; i < objects.Length; i++)
        {

            bpos++;

             a = objects[i].transform.position.ToString();

            Debug.Log(a);


                GameObject newButton = (GameObject)Instantiate(MyButtonPrefab, new Vector3(1, 1, 1), Quaternion.identity);
                newButton.transform.SetParent(ParentPanel, false);
                newButton.transform.localScale = new Vector3(1, 1, 1);

                newButton.transform.position = new Vector3(0, bpos, 0);
                newButton.name = "Camerapos";



                m = newButton.GetComponent<Button>();
                m.onClick.AddListener(() =>
                {


                    Debug.Log(a);



                });



        }


    }



    // Update is called once per frame
    void Update () {

    }



}
  • Possible duplicate of [C# lambda, local variable value not taken when you think?](http://stackoverflow.com/questions/4155691/c-sharp-lambda-local-variable-value-not-taken-when-you-think) – sokkyoku Sep 06 '16 at 08:27

1 Answers1

0

In your lambda function:

m.onClick.AddListener(() =>
{
    Debug.Log(a);
}

a is a reference to the variable declared before. Closures capture references to the variables they use. You've declared a outside of your for loop so each time you loop through it, the same variable is used and your closure captures the same reference.

If you declare a variable inside your loop, you get a new piece of memory every time you run through it and a reference is taken to different chunks of memory.

The following fixes your problem (tested).

string b = a;
m.onClick.AddListener(() =>
{
    Debug.Log(b);
}
sokkyoku
  • 2,161
  • 1
  • 20
  • 22