Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将立方体的位置指定给统一中的按钮单击?_C#_Unity3d - Fatal编程技术网

C# 将立方体的位置指定给统一中的按钮单击?

C# 将立方体的位置指定给统一中的按钮单击?,c#,unity3d,C#,Unity3d,我编写此代码是为了确定场景中立方体的数量,并为每个立方体动态创建一个按钮。当单击按钮时,将显示立方体的位置。但当我运行此代码时,仅显示最终立方体的位置。我在这里做错了什么 using UnityEngine; using System.Collections; using UnityEngine.UI; public class buttons : MonoBehaviour { public GameObject MyButtonPrefab; public RectTra

我编写此代码是为了确定场景中立方体的数量,并为每个立方体动态创建一个按钮。当单击按钮时,将显示立方体的位置。但当我运行此代码时,仅显示最终立方体的位置。我在这里做错了什么

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 () {

    }



}
使用UnityEngine;
使用系统集合;
使用UnityEngine.UI;
公共类按钮:MonoBehavior{
公共游戏对象MyButtonPrefab;
公共事务委员会;
//用于初始化
int-bpos=143;
按钮m;
字符串a;
无效开始(){
//Log(GameObject.FindGameObjectWithTag(“Cube”).transform.position);
GameObject[]objects=GameObject.FindGameObjectsSwithTag(“多维数据集”);
int x=GameObject.FindGameObjectsWithTag(“立方体”).Length;
Debug.Log(x);
for(int i=0;i
{
Debug.Log(a);
});
}
}
//每帧调用一次更新
无效更新(){
}
}

在lambda函数中:

m.onClick.AddListener(() =>
{
    Debug.Log(a);
}
a
是对前面声明的变量的引用。闭包捕获对它们使用的变量的引用。您已经在for循环之外声明了
a
,因此每次循环时,都会使用相同的变量,并且闭包捕获相同的引用

如果在循环中声明一个变量,则每次运行时都会得到一个新的内存块,并引用不同的内存块

以下内容修复了您的问题(已测试)


在lambda函数中:

m.onClick.AddListener(() =>
{
    Debug.Log(a);
}
a
是对前面声明的变量的引用。闭包捕获对它们使用的变量的引用。您已经在for循环之外声明了
a
,因此每次循环时,都会使用相同的变量,并且闭包捕获相同的引用

如果在循环中声明一个变量,则每次运行时都会得到一个新的内存块,并引用不同的内存块

以下内容修复了您的问题(已测试)


它只给出一个值。它只给出一个值