随机.范围是选择两个游戏对象而不是一个



我已经为一个问题生成器创建了代码,但我的random.range不时会从20个游戏对象中选择两个。代码应该可以工作,但有什么原因导致它选择了两个。这是一个统一的bug还是代码本身?我已经创建了一个列表,然后当选择号码时,它应该将其从列表中删除,以防止任何号码重复(同样的问题问了两次(。

public GameObject Question1, Question2, Question3, Question4, Question5, Question6, Question7, Question8, Question9, Question10, Question11, Question12, Question13, Question14, Question15, Question16, Question17, Question18, Question19, Question20;
public GameObject VerdictGood, VerdictBad;
public GameObject box_QA;
public courtDialogue _courtDialogue;
List<int> list = new List<int>();
private int i, index, calculate, maxquestions;
public bool neverdone;

public void Start()
{
box_QA.SetActive(false);
calculate = 0;
for (int n = 1; n < 21; n++)
{
list.Add(n);
}
}
void Update()
{
DeleteQuestions();
}
public void CheckQuestion()
{
index = Random.Range(0, list.Count - 1);
i = list[index];
Debug.Log(i);
list.RemoveAt(index);
}
public void WhatQuestion()
{
CheckQuestion();
if (i == 1)
{
Question1.SetActive(true);
Question2.SetActive(false);
Question3.SetActive(false);
Question4.SetActive(false);
Question5.SetActive(false);
Question6.SetActive(false);
Question7.SetActive(false);
Question8.SetActive(false);
Question9.SetActive(false);
Question10.SetActive(false);
Question11.SetActive(false);
Question12.SetActive(false);
Question13.SetActive(false);
Question14.SetActive(false);
Question15.SetActive(false);
Question16.SetActive(false);
Question17.SetActive(false);
Question18.SetActive(false);
Question19.SetActive(false);
Question20.SetActive(false);
}
}
void DeleteQuestions()
{
if (maxquestions == 10)
{
StopCoroutine("CheckQuestion");
StopCoroutine("WhatQuestion");
Destroy(Question1);
Destroy(Question2);
Destroy(Question3);
Destroy(Question4);
Destroy(Question5);
Destroy(Question6);
Destroy(Question7);
Destroy(Question8);
Destroy(Question9);
Destroy(Question10);
Destroy(Question11);
Destroy(Question12);
Destroy(Question13);
Destroy(Question14);
Destroy(Question15);
Destroy(Question16);
Destroy(Question17);
Destroy(Question18);
Destroy(Question19);
Destroy(Question20);
if (calculate > 7)
{
JudgeImage.GetComponent<Image>().color = new Color32(6, 255, 0, 255);
VerdictGood.SetActive(true);
Debug.Log("Not Quilty");
}
else
{
JudgeImage.GetComponent<Image>().color = new Color32(255, 0, 0, 255);
VerdictBad.SetActive(true);
Debug.Log("Not Quilty");
}
}
}

控制台输出

public GameObject judgeFace;
public GameObject prosecutorFace;
public GameObject clientFace;
public GameObject courtQuestions;
public GameObject healthBar;
public int courtIntroCount;             //This variable keeps track of whose line is next in the court dialogue scene.
public GameObject fullTextBox;
public Text nameText;
public Text mainText;
public float delay = 0.1f;
public string fullText;
private string currentText = "";
public GameManager2 _gameManager2;
// Use this for initialization
void Start ()
{
//  courtQuestions.SetActive(false);
fullTextBox.SetActive(false);
healthBar.SetActive(false);
Invoke("CourtIntro1", 3);

}
IEnumerator ShowText()
{
for (int i = 0; i < fullText.Length; i++)
{
currentText = fullText.Substring(0, i);
mainText.GetComponent<Text>().text = currentText;
yield return new WaitForSeconds(delay);
}
}

// Update is called once per frame
public void CourtButtons()
{
if (courtIntroCount == 1)
CourtIntro2();
else if (courtIntroCount == 2)
CourtIntro3();
else if(courtIntroCount == 3)
CourtIntro4();
else if(courtIntroCount == 4)
CourtIntro5();
else if (courtIntroCount == 5)
CourtIntroEND();
// This needs to have a way of checking which question has been disabled after the answer has been selected
}

//  COURT DIALOGUE _ INTRO SEQUENCE
public void CourtIntro1()
{
courtIntroCount = 1;
fullTextBox.SetActive(true);
judgeFace.SetActive(true);
nameText.text = "Judge";
StartCoroutine(ShowText());
currentText = "Court is now in-session.  All rise.";
}
public void CourtIntro2()
{
courtIntroCount = 2;
fullTextBox.SetActive(true);
nameText.text = "Judge";
StartCoroutine(ShowText());
fullText = "Now, you, lawyer.  Do you solemnly and sincerely and truly declare and affirm that the evidence you shall give shall be the truth, the whole truth and nothing but the truth?.";
}
public void CourtIntro3()
{
courtIntroCount = 3;
fullTextBox.SetActive(true);
nameText.text = "Judge";
StartCoroutine(ShowText());
fullText = "... Very good.  Now, the prosecution would like to begin by asking the defence a number of questions..";
}
public void CourtIntro4()
{
courtIntroCount = 4;
fullTextBox.SetActive(true);
judgeFace.SetActive(false);
prosecutorFace.SetActive(true);
nameText.text = "Prosecutor";
StartCoroutine(ShowText());
fullText = "I would, Your Honour.  I hope the defence will be able to answer them accurately and appropriately for you and the jury..";
}
public void CourtIntro5()
{
courtIntroCount = 5;
fullTextBox.SetActive(true);
prosecutorFace.SetActive(false);
clientFace.SetActive(true);
nameText.text = "Ellen";
StartCoroutine(ShowText());
fullText = "This is it!  You'll need to convince the judge and jury that I'm not guilty.  Best of luck!.";
}
public void CourtIntroEND()
{
courtIntroCount = 10;
clientFace.SetActive(false);
fullTextBox.SetActive(false);
//courtQuestions.SetActive(true);
healthBar.SetActive(true);
_gameManager2.box_QA.SetActive(true);
_gameManager2.WhatQuestion();

}

如果能获得更多关于它是如何构造的信息,那就太好了,但根据我在这里看到的,WhatQuestion((方法似乎需要知道变量"I"是什么。这通常是通过创建接受参数和返回值的方法来完成的。对于这个例子,看起来CheckQuestion((方法应该返回一个值"i":

public int CheckQuestion()
{
//do some stuff
return i;
}

然后,你的WhatQuestion((方法应该调用CheckQuestion

public void WhatQuestion()
{
i = CheckQuestion();
if (i == 1)
{
//Do your stuff
}
}

您可能还需要一种方法来取消激活所有其他问题,以便一次只激活一个问题。类似的东西

foreach (var question in QuestionList)
{
question.SetActive(false);
}

然后,激活一个问题:

QuestionList[i].SetActive(true);

希望这些信息能有所帮助,这是我对这里所呈现内容的最佳猜测。

最新更新