Hi,
I am trying to write a script that controls buttons and checks if they are pressed in the right sequence to activate game object (Image)
I would appreciate help with this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SequenceMiniGame : MonoBehaviour
{
public GameObject [] buttons;
public GameObject winImage;
public void buttonClick(object myObject)
{
// buttons[buttonCounter]
// buttonCounter++;
}
public void EndGame()
{
if (true)
{
winImage.SetActive(true);
}
else
{
//reset;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SequenceMiniGame : MonoBehaviour
{
public Button[] buttons;
public GameObject winImage;
private int nextButtonIndex;
private void Start()
{
for( int i = 0 ; i < buttons.Length ; ++i)
{
int buttonIndex = i; // Keep this line, it's essential
buttons[buttonIndex].onClick.AddListener( () => OnButtonClicked( buttonIndex ) );
}
}
private void OnButtonClicked( int buttonIndex )
{
// Wrong button clicked
if( nextButtonIndex != buttonIndex )
{
Debug.LogWarning("The wrong button has been clicked!");
nextButtonIndex = 0 ; // Reset the sequence
return;
}
// Last button clicked?
else if( ++nextButtonIndex == buttons.Length )
{
Debug.Log("You won!");
EndGame();
}
else
{
Debug.Log("You are on the right track!");
}
}
public void EndGame()
{
winImage.SetActive(true);
// Remove the listeners from the buttons
for( int i = 0 ; i < buttons.Length ; ++i)
{
buttons*.onClick.RemoveAllListeners();*
}
}
}
easiest way i would do this is something like
bool thing = false, thing_2 = false;
if(buttons[0].pressed()) thing = true;
if(buttons[1].pressed() && thing) thing_2 = true; /*[etc]*/
not the best nor the most correct way, but it works