I want to crate a minigame based on guess the order of the flickering buttons. First I add in a vector the flickering buttons order and the I add the user taps on buttons in another array. In the end if the vector values are equal user win the game. The problem is at the end no matter what values are in vectors, there will be a win anyway. I can't find the problem. Here is my whole script:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.ShortcutManagement;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Script : MonoBehaviour
{
[SerializeField] private GameObject[] buttons;
private string[] culori = new string[3];
private int[] culoriINT = new int[3];
private string[] culoriComparare = new string[3];
private int[] culoriComparareINT = new int[3];
int index = 0;
int index2 = 0;
int indexCourt = 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 ) {
if (index < 10)
{
var a = UnityEngine.Random.Range(0, buttons.Length);
buttons[a].GetComponent<Image>().color = Color.green;
var introducere = buttons[a].GetComponent<IndexButtons>().index;
culoriINT[index] = introducere;
index ;
yield return new WaitForSeconds(0.5f);
buttons[a].GetComponent<Image>().color = Color.white;
}
else
{
Debug.Log("index depasit");
}
Debug.Log("verde");
yield return new WaitForSeconds(1f);
indexCourt ;
}
}
public void OnButtonClick()
{
index2 ;
}
public void numePeClick()
{
string a = EventSystem.current.currentSelectedGameObject.name;
culoriComparareINT[index2] = Convert.ToInt32(a);
culoriComparare[index2] = a;
Debug.Log("Cuvant adaugat");
}
public void CheckWin()
{
for (var i = 0; i < culoriINT.Length; i )
{
if (culoriINT[i] != culoriComparareINT[i])
{
win = false;
}
else
{
win = true;
}
}
if (win == false)
{
Debug.Log("Ai pierdut!");
}
else
{
Debug.Log("Ai castigat!");
}
}
}
CodePudding user response:
This is because in the for-loop of CheckWin
, you are not keeping the value win = false
. At the next iteration it might be set to true
again. So, the end result depends only on the last element in the array.
You must exit the loop as soon as a non-matching entry is found. You can do so with the break
statement.
public void CheckWin()
{
win = true;
for (int i = 0; i < culoriINT.Length; i )
{
if (culoriINT[i] != culoriComparareINT[i])
{
win = false;
break; // Exit the loop, the arrays are not equal.
}
}
if (win)
{
Debug.Log("Ai castigat!");
}
else
{
Debug.Log("Ai pierdut!");
}
}
This solution would also work if the break
was not there, as win
keeps its value false
once it has been set. However, there is no point in testing the rest of the array at this point, as the inequality of the arrays is already given when one element is unequal.
I also inverted the if-statement at the end of the method. Code is usually more understandable if a condition asks a positive question and the if-part handles the positive outcome and the else the negative one.
Also, you can test a Boolean variable with if(win)
or if(!win)
instead of if(win == true)
or if(win == false)
.
CodePudding user response:
Your CheckWin method will always return only the value of the last comparison. Change to
public void CheckWin()
{
win = true;
for (var i = 0; i < culoriINT.Length; i )
{
win &= culoriINT[i] == culoriComparareINT[i];
}
if (win == false)
{
Debug.Log("Ai pierdut!");
}
else
{
Debug.Log("Ai castigat!");
}
}