在用户生成整个过程之后,数组值被设置为零



我想创建一个迷你游戏,从游戏对象数组中随机选择按钮,并将值存储在数组中。在第一步完成后,用户需要点击相同的按钮,否则他将失败。问题是,当我想比较这两个数组的值时,索引0-2中的每个值在两个数组中都设置为0。我试着调试添加部分,这很好,即使在编辑器中我也可以看到存储的值。这是一张照片:存储对象编辑器。我甚至放了两个for循环来检查游戏中数组中的值,而不是得到3个打印,我得到了其中的6个,前3个表示正确的值,另外3个的值=0(这适用于两个数组(。在我的CheckWin((中,结果将始终为true,因为两个数组中每个位置的比较值都为0。我不知道是什么迫使数组的所有组件都设置为零。这是脚本:

public class Script : MonoBehaviour
{
[SerializeField] private GameObject[] buttons;
[SerializeField] private int[] culoriINT;
[SerializeField] private int[] culoriComparareINT;
int index = 0;
int index2 = 0;
private bool win = false;

private void Start()
{
StartCoroutine(ChangeColors2());
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.P))
{
for (int i = 0; i < culoriINT.Length; i++)
{
Debug.Log("INT vector[" + i + "]: " + culoriINT[i]);
}
}
if (Input.GetKeyDown(KeyCode.W))
{
for (int i = 0; i < culoriComparareINT.Length; i++)
{
Debug.Log("Al doilea vector[" + i + "]: " + culoriComparareINT[i]);
}
}
}

IEnumerator ChangeColors2()
{
yield return new WaitForSeconds(1f);
for (int j = 0; j < buttons.Length; j++)
{
var randomBtn = UnityEngine.Random.Range(0, buttons.Length);
buttons[randomBtn].GetComponent<Image>().color = Color.green;
var introducereIndex = buttons[randomBtn].GetComponent<IndexButtons>().index;
culoriINT[index] = introducereIndex;
Debug.Log($"Index adaugat {introducereIndex} total {culoriINT.Length}");
index++;
yield return new WaitForSeconds(0.5f); //seteaza coloare alb pe acelas buton in cazu in care nimereste acelas sa se vada
buttons[randomBtn].GetComponent<Image>().color = Color.white;
Debug.Log("verde");
yield return new WaitForSeconds(1f);
}
}

public void OnButtonClick()
{
index2++;
}
public void numePeClick()
{
if (index2 < buttons.Length)
{
string a = EventSystem.current.currentSelectedGameObject.name;
culoriComparareINT[index2] = Convert.ToInt32(a);
Debug.Log($"Index adaugat {Convert.ToInt32(a)} total {culoriComparareINT.Length}");
}
else
{
Debug.Log("Array plin");
}
}
public void CheckWin()
{
win = true;

for (var i = 0; i < culoriINT.Length; i++)
{
if (culoriINT[i] != culoriComparareINT[i])
{
win = false;
break;
}
else
{
win = true;
}
}
if (win)
{
Debug.Log("Ai castigat!");
}
else
{
Debug.Log("Ai pierdut!");
}
}
}

听起来场景中有两个相同Script组件的实例。其中一个具有正确值3,2,3,另一个具有错误值0,0,0。这就是为什么六个值被打印到控制台,而不是通过一个输入打印三个值。

最新更新