Random.Range选择两个游戏对象而不是一个

时间:2018-11-16 13:42:57

标签: c# unity3d random

我已经为问题生成器创建了代码,但是我的random.range时不时地从我的两个游戏对象中挑选出20个。该代码应该可以工作,但是某种原因导致它选择了两个。是统一还是程序本身的错误?我创建了一个列表,然后在选择了号码后,应该将其从列表中删除,以防止任何号码重复(相同的问题,问了两次)。

 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");
        }
    }
}

Console Output

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();


}

1 个答案:

答案 0 :(得分:1)

了解有关其结构的更多信息会很好,但是根据我在这里看到的内容,您的WhatQuestion()方法似乎需要知道变量'i'是什么。这通常是通过创建接受参数并返回值的方法来完成的。对于此示例,您的CheckQuestion()方法看起来应该返回值'i':

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

然后,您的WhatQuestion()方法应调用CheckQuestion()以获取“ i”:

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

您可能还需要一种停用所有其他问题的方法,以便一次仅激活一个。像

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

然后,激活一个问题:

QuestionList[i].SetActive(true);

希望此信息有所帮助,这是我的最佳猜测。