Help guys i made a chest ui and chest manager. i got 4 buttons but these buttons can be click at the same. How can i make it only one can be click at the same time. These are my codes.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class ChestUI : MonoBehaviour { public PlayerHealth healthbar; public PlayerHealthBar1 health; private PlayerMovement playermovemnt; public PlayerMovement2 playermove; [SerializeField] private ChestManager chestManager; [SerializeField] private Text multipleText; [SerializeField] private List butns; [SerializeField] private Color correctColl, wrongColl, normalColl; public GameObject unlockmode; public GameObject overmode; public GameObject boxs; public GameObject btns; public AudioSource opening;
public int Totalwrongs;
public int rights;
public GameObject swapOpen;
public GameObject swapClose;
public GameObject Key;
public static int scorevalue = 0;
public int wrong;
public Text textbox;
public Text wrongbox;
public Text infotext;
private Questionaire questions;
private bool answered;
// Start is called before the first frame update
void Awake()
{
for (int i = 0; i < butns.Count; i++)
{
Button localBtn = butns[i];
localBtn.onClick.AddListener(() => OnClick(localBtn));
}
}
private void Update()
{
infotext.text = rights + "pts to get the key";
textbox.text = "Score: " + scorevalue;
wrongbox.text = "Wrong: " + wrong + "/" + Totalwrongs;
butns[1].gameObject.SetActive(true);
butns[0].gameObject.SetActive(true);
}
public void SetQuestion(Questionaire question)
{
this.questions = question;
switch (question.questiontypes)
{
case QuestionTypes.TEXT:
break;
}
multipleText.text = question.questionInform;
List<string> answerList = ShuffleList.ShuffleListItems<string>(question.options);
for (int i = 0; i < butns.Count; i++)
{
butns[i].GetComponentInChildren<Text>().text = answerList[i];
butns[i].name = answerList[i];
butns[i].image.color = normalColl;
}
answered = false;
}
private void OnClick(Button btn)
{
if (!answered)
{
bool val = chestManager.Answer(btn.name);
if (val)
{
scorevalue = scorevalue + 10;
textbox.text = "Score: " + scorevalue;
if (scorevalue == rights)
{
wrong = 0;
scorevalue = 0;
textbox.text = "Score: " + scorevalue;
wrongbox.text = "Wrong: " + wrong + "/" + Totalwrongs;
unlockmode.SetActive(false);
swapOpen.SetActive(true);
swapClose.SetActive(false);
Key.SetActive(true);
overmode.SetActive(false);
opening.Play();
btns.SetActive(true);
boxs.SetActive(true);
}
btn.image.color = correctColl;
}
else
{
btn.image.color = wrongColl;
wrong = wrong + 1;
wrongbox.text = "Wrong: " + wrong + "/" + Totalwrongs;
if (wrong == Totalwrongs)
{
healthbar.currenthealth -= 20;
wrong = 0;
scorevalue = 0;
textbox.text = "Score: " + scorevalue;
wrongbox.text = "Wrong: " + wrong + "/" + Totalwrongs;
overmode.SetActive(false);
}
else if(wrong == Totalwrongs)
{
health.currenthealth -= 20;
wrong = 0;
scorevalue = 0;
textbox.text = "Score: " + scorevalue;
wrongbox.text = "Wrong: " + wrong + "/" + Totalwrongs;
overmode.SetActive(false);
}
}
}
}
public void button1()
{
butns[1].gameObject.SetActive(false);
}
public void button2()
{
butns[0].gameObject.SetActive(false);
}
}
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ChestManager : MonoBehaviour { [SerializeField] private ChestUI chestUI;
[SerializeField]
private List<Questionaire> questions;
private int index;
private Questionaire selectedQuestions;
// Start is called before the first frame update
void Start()
{
SelectQuestion();
}
void SelectQuestion()
{
int val = Random.Range(0, questions.Count);
selectedQuestions = questions[val];
index = val;
chestUI.SetQuestion(selectedQuestions);
}
public bool Answer(string answered)
{
bool correctAnswers = false;
if (answered == selectedQuestions.correctAns)
{
correctAnswers = true;
}
else
{
//no
}
Invoke("SelectQuestion", 0.4f);
return correctAnswers;
}
}
[System.Serializable] public class Questionaire { public string questionInform; public List options; public string correctAns; public QuestionTypes questiontypes; } [System.Serializable] public enum QuestionTypes { TEXT }
Answer by kbop2000 · Jul 13, 2020 at 09:47 AM
void Awake()
{
for (int i = 0; i < butns.Count; i++)
{
Button localBtn = butns[i];
localBtn.onClick.AddListener(() => OnClick(localBtn, i));
}
}
private void OnClick(Button btn, int index)
{ ... }
If you are using the same script for the button, you can distinguish it by adding a parameter