【发布时间】:2019-01-16 11:18:09
【问题描述】:
我有两个长度相同的 GameObject 数组。我正在尝试将 array1 的相同值复制到 array2。 我尝试使用 system.Array.copy(Array1,Array2,4) 并且还尝试了 Array1= Array2 但不工作。 这两个数组包含 4 个按钮,每个按钮都有子文本。我想显示分配给这些按钮的 4 个答案,并同时将相同的答案复制到另外 4 个按钮。有点像双重播放器。 有人可以帮忙吗?
public class DuelMode : MonoBehaviour{
public static DuelMode instance;
// these are the question values a and b
private int a, b, a1, b1;
//the variable for answer value
[HideInInspector] public int answer;
//varible whihc will assign ans to any one of the 4 answer button
private int locationOfAnswer;
//ref to the button
public GameObject[] ansButtons;
private GameObject[] ansButtonsDuel;
//ref to image symbol so player can know which operation is to be done
public Image mathSymbolObject;
//ref to all the symbol sprites whihc will be used in above image
public Sprite[] mathSymbols;
//get the tag of button
public string tagOfButton;
//ref to text in scene where we will assign a and b values of question
public Text valueA, valueB, valueA1, valueB1;
void Awake()
{
MakeInstance();
}
//method whihc make this object instance
void MakeInstance()
{
if (instance == null)
{
instance = this;
}
}
//at start we need to do few basic setups
void Start()
{
//we put the location value in tag of button variable
tagOfButton = locationOfAnswer.ToString();
MathsProblem();
}
//this method keeps the track of mode
// Update is called once per frame
void Update()
{
tagOfButton = locationOfAnswer.ToString();
}
//Below code is for maths calculation
public void MathsProblem()
{
bool roundActive = true;
}
//this methode perform Multiplication process
void MultiplicationMethod()
{
bool reloop;
bool[] numbers = new bool[301];
b = b1 = Random.Range(1, 10);
locationOfAnswer = Random.Range(0, ansButtons.Length);
answer = a * b;
numbers[answer] = true;
if (valueA != null && valueB != null && valueA1 != null && valueB1 != null)
{
valueA.text = a.ToString();
valueB.text = b.ToString();
valueA1.text = a.ToString();
valueB1.text = b.ToString();
}
mathSymbolObject.sprite = mathSymbols[0];
for (int i = 0; i < ansButtons.Length; i++)
{
if (i == locationOfAnswer)
{
ansButtons[i].GetComponentInChildren<Text>().text = "" + answer;
}
else
{
// the below code make sure that all the values assigned to the ans button are within the range
if (a * b <= 100)
{
ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(1, 101);
}
else if (a * b <= 200 & a * b > 100)
{
ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(101, 201);
}
else if (a * b <= 300 & a * b > 200)
{
ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(201, 301);
}
else if (a * b <= 400 & a * b > 300)
{
ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(301, 401);
}
while (ansButtons[i].GetComponentInChildren<Text>().text == "" + answer)
{
ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(1, 401);
}
}
}
}
}
【问题讨论】:
-
您能否发布更多代码以便为您提供帮助
-
你看过
Object.Instantiate()吗?您应该能够在循环中使用它来将每个元素克隆到一个新数组中。 -
那些数组只保存对 GameObjects 的引用......所以将引用复制到一个新数组中对于拥有更多 GameObjects 没有多大帮助......见上文
-
是的,正在更新我的代码
-
@Sarita 你能把它分解到基本部分吗?我也看不到你试图复制/克隆数组的位置