【问题标题】:Random.Range is picking two gameobjects instead of oneRandom.Range 选择两个游戏对象而不是一个
【发布时间】:2018-11-16 13:42:56
【问题描述】:

我已经为问题生成器创建了代码,但我的 random.range 时不时地选择我的两个游戏对象(共 20 个)。该代码应该可以工作,但是某些原因导致它选择了两个。是统一的错误还是代码本身的错误? I have created a list and then when the number is picked it should the delete it from the list preventing any number duplication's(same question asked twice).

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


}

【问题讨论】:

  • it picks two 是什么意思?
  • 所以我有一个包含 20 个以上子游戏对象的游戏对象,这些游戏对象是包含答案的问题。每次我按下按钮从所有禁用的游戏对象中随机激活一个游戏对象时,它有时会激活两个而不是一个
  • 您的CheckQuestion 函数中有一条日志语句。它是否表明该函数被调用了两次?这几乎肯定是问题所在。在不知道处理按钮的其余代码如何工作的情况下,很难就如何防止它被触发两次提供建议。
  • 我附上了一张图片,以便您查看控制台中发生的情况。
  • 我没有解决方案,但你应该重构你的代码。 1. 将所有问题放在一个列表中并遍历列表,而不是手动设置所有对象。 2. CheckQuestions 应该返回 i 而不是全局设置。我想问题是你没有以某种方式禁用正确的数字。您还应该发布“deleteQuestions”方法

标签: c# unity3d random


【解决方案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);

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

【讨论】:

  • 我刚刚尝试了您的建议,但不幸的是结果相同
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 2015-04-18
  • 1970-01-01
相关资源
最近更新 更多