C# 如何将一个数组中的对象中的值分配到另一个数组中?
在这里,我可以问很多关于数组的问题,但我会坚持一个具体的问题。如果您在下面的代码中看到其他低效的地方,请知道我并不是要让您承担修复这些问题的挑战 我的具体问题是,我想在锯齿状菜单[][]数组的整个长度上循环,然后从按钮[]数组中提取一个Vector3值,并将其分配给Vector3[]数组 我不知道为什么我不能做我在下面发布的事情,但我得到: 1) NullReferenceException错误(在按钮上[n]=按钮[n].transform.position 2) “字段'MatrixPicker.buttonPos'从未分配给,并且始终具有其默认值'null'” …当我尝试时。所有my button[]游戏对象都位于属于Unity的公共字段中 这是我的密码:C# 如何将一个数组中的对象中的值分配到另一个数组中?,c#,unity3d,C#,Unity3d,在这里,我可以问很多关于数组的问题,但我会坚持一个具体的问题。如果您在下面的代码中看到其他低效的地方,请知道我并不是要让您承担修复这些问题的挑战 我的具体问题是,我想在锯齿状菜单[][]数组的整个长度上循环,然后从按钮[]数组中提取一个Vector3值,并将其分配给Vector3[]数组 我不知道为什么我不能做我在下面发布的事情,但我得到: 1) NullReferenceException错误(在按钮上[n]=按钮[n].transform.position 2) “字段'MatrixPick
public class MatrixPicker : MonoBehaviour {
string[][] menu;
public GameObject[] buttons;
private Vector3[] buttonPos;
void Start () {
menu = new string[][]{
new string[]{"a"},
new string[]{"b"},
new string[]{"c", "d", "e"},
new string[]{"f", "g", "h"}
};
int tot = 0;
for (int i = 0; i < menu.Length; i++){
string[] innerArray = menu[i];
for (int a = 0; a < innerArray.Length; a++){
tot++;
int n = tot-1;
buttonPos[n] = buttons[n].transform.position;
}
}
}
//other code
}
公共类MatrixPicker:MonoBehavior{
字符串[][]菜单;
公共游戏对象[]按钮;
私有向量3[]按钮;
无效开始(){
菜单=新字符串[][]{
新字符串[]{“a”},
新字符串[]{“b”},
新字符串[]{“c”、“d”、“e”},
新字符串[]{“f”、“g”、“h”}
};
int-tot=0;
for(int i=0;i
在尝试设置其元素之前,需要初始化按钮。例如:
private Vector3[] buttonPos = new Vector3[menu.Length]; //Create an array that can contain one vector for each menu item
初始化buttonPos
数组后,可以使用循环初始化每个数组元素,这是一个单独的操作。在尝试设置其元素之前,需要初始化buttonPos
。例如:
private Vector3[] buttonPos = new Vector3[menu.Length]; //Create an array that can contain one vector for each menu item
一旦你初始化了按钮POS
数组,你就可以使用你的循环来初始化每个数组元素,这是一个独立的操作。据我所知,你没有绑定游戏对象,因此它总是指向零(null)。您可以尝试使用GameObject.FindWithTag绑定对象吗?
您还需要初始化阵列
例如:
buttons = GameObject.FindGameObjectsWithTag("YOUR_OBJECT'S_TAG");
我还建议使用foreach而不是for循环:
for(Item item: list)
{
//Your code;
}
希望有帮助 据我所知,您没有绑定GameObject,因此它总是指向nothing(null)。您可以尝试使用GameObject.FindWithTag绑定对象吗?
您还需要初始化阵列
例如:
buttons = GameObject.FindGameObjectsWithTag("YOUR_OBJECT'S_TAG");
我还建议使用foreach而不是for循环:
for(Item item: list)
{
//Your code;
}
希望有帮助 buttonPos是一个私有变量,因此我可以推断您在使用前没有初始化它。下面的代码将修复此错误
void Start () {
menu = new string[][]{
new string[]{"a"},
new string[]{"b"},
new string[]{"c", "d", "e"},
new string[]{"f", "g", "h"}
};
int tot = 0;
int arrayLength = 0;
foreach(string[] array in menu)
{
arrayLength += array.Length;
}
//initialise it here
buttonPos = new Vector3[arrayLength];
for (int i = 0; i < menu.Length; i++){
string[] innerArray = menu[i];
for (int a = 0; a < innerArray.Length; a++){
tot++;
int n = tot-1;
//then you can assign it here
buttonPos[n] = buttons[n].transform.position;
}
}
}
使用列表的一维buttonPos数组的替代方法
注意如何在底部匹配菜单中将其转换为锯齿状数组
公共类MatrixPicker:MonoBehavior{
string[][] menu;
public GameObject[] buttons;
private List<Vector3[]> buttonPos = new List<Vector3[]>();
void Start () {
menu = new string[][]{
new string[]{"a"},
new string[]{"b"},
new string[]{"c", "d", "e"},
new string[]{"f", "g", "h"}
};
int tot = 0;
for (int i = 0; i < menu.Length; i++){
string[] innerArray = menu[i];
//initialise an array here to store this iteration of button positions
bPosArray = new Vector3[innerArray.Length];
for (int a = 0; a < innerArray.Length; a++){
tot++;
int n = tot-1;
//set the current element as usual
bPosArray[n] = buttons[n].transform.position;
}
//push back the new array into a list
buttonPos.add(bPosArray);
}
}
//other code
void SomeOtherCode()
{
//if you need an array, just use
Vector3[] anArrayOfTheButtonPosList = buttonPos.ToArray();
//this will return a jagged array matching the menu array, with positions
}
}
string[]菜单;
公共游戏对象[]按钮;
私有列表按钮POS=新列表();
无效开始(){
菜单=新字符串[][]{
新字符串[]{“a”},
新字符串[]{“b”},
新字符串[]{“c”、“d”、“e”},
新字符串[]{“f”、“g”、“h”}
};
int-tot=0;
for(int i=0;i
buttonPos是一个私有变量,因此我可以推断您在使用前没有初始化它。下面的代码将修复此错误
void Start () {
menu = new string[][]{
new string[]{"a"},
new string[]{"b"},
new string[]{"c", "d", "e"},
new string[]{"f", "g", "h"}
};
int tot = 0;
int arrayLength = 0;
foreach(string[] array in menu)
{
arrayLength += array.Length;
}
//initialise it here
buttonPos = new Vector3[arrayLength];
for (int i = 0; i < menu.Length; i++){
string[] innerArray = menu[i];
for (int a = 0; a < innerArray.Length; a++){
tot++;
int n = tot-1;
//then you can assign it here
buttonPos[n] = buttons[n].transform.position;
}
}
}
使用列表的一维buttonPos数组的替代方法
注意如何在底部匹配菜单中将其转换为锯齿状数组
公共类MatrixPicker:MonoBehavior{
string[][] menu;
public GameObject[] buttons;
private List<Vector3[]> buttonPos = new List<Vector3[]>();
void Start () {
menu = new string[][]{
new string[]{"a"},
new string[]{"b"},
new string[]{"c", "d", "e"},
new string[]{"f", "g", "h"}
};
int tot = 0;
for (int i = 0; i < menu.Length; i++){
string[] innerArray = menu[i];
//initialise an array here to store this iteration of button positions
bPosArray = new Vector3[innerArray.Length];
for (int a = 0; a < innerArray.Length; a++){
tot++;
int n = tot-1;
//set the current element as usual
bPosArray[n] = buttons[n].transform.position;
}
//push back the new array into a list
buttonPos.add(bPosArray);
}
}
//other code
void SomeOtherCode()
{
//if you need an array, just use
Vector3[] anArrayOfTheButtonPosList = buttonPos.ToArray();
//this will return a jagged array matching the menu array, with positions
}
}
string[]菜单;
公共游戏对象[]按钮;
私有列表按钮POS=新列表();
无效开始(){
菜单=新字符串[][]{
新字符串[]{“a”},
新字符串[]{“b”},
新字符串[]{“c”、“d”、“e”},
新字符串[]{“f”、“g”、“h”}
};
int-tot=0;
for(int i=0;i
这很有效
string[][] menu;
private int X = 0;
private int Y = 0;
public GameObject[] buttons;
void Start () {
menu = new string[][]{
new string[]{"a"},
new string[]{"b"},
new string[]{"c", "d", "e"},
new string[]{"f", "g", "h"}
};
print("menu Length = "+menu.Length);
int tot = 0;
for (int i = 0; i < menu.Length; i++){
string[] innerArray = menu[i];
for (int a = 0; a < innerArray.Length; a++){
tot++;
}
}
int n = tot-1;
Vector3[] buttonPos = new Vector3[n];
int count = 0;
foreach(Vector3 button in buttonPos){
buttonPos[count] = buttons[count].transform.position;
print(buttonPos[count]);
count++;
}
}
string[]菜单;
私有整数X=0;
私有整数Y=0;
公共游戏对象[]按钮;
无效开始(){
菜单=新字符串[][]{
新字符串[]{“a”},
新字符串[]{“b”},
新字符串[]{“c”、“d”、“e”},
新字符串[]{“f”、“g”、“h”}
};
打印(“菜单长度=”+菜单长度);
int-tot=0;
对于(int i=0;i