Random.Range is picking two gameobjects instead of one





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















I have created code for a question generator but every now and then my random.range is picking two of my gameobjects(out of 20). The code should work but something is causing it to pick two. Is it a bug with unity or the code itself? 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();


}









share|improve this question




















  • 3





    What do you mean by it picks two?

    – tkausl
    Nov 16 '18 at 13:44











  • So I have a gameobject that contains 20 more sub-gameobjects these gameobjects are questions containing answers. Every time I press a button to activate a gameobject randomly from all the gameobjects that are disabled it sometimes activates two instead of one

    – BlindGuardian
    Nov 16 '18 at 13:50











  • You have a log statement in your CheckQuestion function. Does it indicate the function is being called twice? This is almost certainly the issue. Without knowing how the rest of your code dealing with the button works it's hard to give advice on how to prevent it from being triggered twice.

    – Retired Ninja
    Nov 16 '18 at 13:54











  • I have attached an Image so you can see what happens in the console.

    – BlindGuardian
    Nov 16 '18 at 13:58






  • 2





    I dont have a solution, but you should refactor your code. 1. Put all questions inside a list and iterate over the list instead of setting all the objects via hand. 2. CheckQuestions should return i instead of setting it globally. I guess the problem occurs that you dont disable the correct number somehow. You should also post "deleteQuestions" method

    – Loading
    Nov 16 '18 at 14:01


















-1















I have created code for a question generator but every now and then my random.range is picking two of my gameobjects(out of 20). The code should work but something is causing it to pick two. Is it a bug with unity or the code itself? 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();


}









share|improve this question




















  • 3





    What do you mean by it picks two?

    – tkausl
    Nov 16 '18 at 13:44











  • So I have a gameobject that contains 20 more sub-gameobjects these gameobjects are questions containing answers. Every time I press a button to activate a gameobject randomly from all the gameobjects that are disabled it sometimes activates two instead of one

    – BlindGuardian
    Nov 16 '18 at 13:50











  • You have a log statement in your CheckQuestion function. Does it indicate the function is being called twice? This is almost certainly the issue. Without knowing how the rest of your code dealing with the button works it's hard to give advice on how to prevent it from being triggered twice.

    – Retired Ninja
    Nov 16 '18 at 13:54











  • I have attached an Image so you can see what happens in the console.

    – BlindGuardian
    Nov 16 '18 at 13:58






  • 2





    I dont have a solution, but you should refactor your code. 1. Put all questions inside a list and iterate over the list instead of setting all the objects via hand. 2. CheckQuestions should return i instead of setting it globally. I guess the problem occurs that you dont disable the correct number somehow. You should also post "deleteQuestions" method

    – Loading
    Nov 16 '18 at 14:01














-1












-1








-1








I have created code for a question generator but every now and then my random.range is picking two of my gameobjects(out of 20). The code should work but something is causing it to pick two. Is it a bug with unity or the code itself? 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();


}









share|improve this question
















I have created code for a question generator but every now and then my random.range is picking two of my gameobjects(out of 20). The code should work but something is causing it to pick two. Is it a bug with unity or the code itself? 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();


}






c# unity3d random






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 14:26







BlindGuardian

















asked Nov 16 '18 at 13:42









BlindGuardianBlindGuardian

137




137








  • 3





    What do you mean by it picks two?

    – tkausl
    Nov 16 '18 at 13:44











  • So I have a gameobject that contains 20 more sub-gameobjects these gameobjects are questions containing answers. Every time I press a button to activate a gameobject randomly from all the gameobjects that are disabled it sometimes activates two instead of one

    – BlindGuardian
    Nov 16 '18 at 13:50











  • You have a log statement in your CheckQuestion function. Does it indicate the function is being called twice? This is almost certainly the issue. Without knowing how the rest of your code dealing with the button works it's hard to give advice on how to prevent it from being triggered twice.

    – Retired Ninja
    Nov 16 '18 at 13:54











  • I have attached an Image so you can see what happens in the console.

    – BlindGuardian
    Nov 16 '18 at 13:58






  • 2





    I dont have a solution, but you should refactor your code. 1. Put all questions inside a list and iterate over the list instead of setting all the objects via hand. 2. CheckQuestions should return i instead of setting it globally. I guess the problem occurs that you dont disable the correct number somehow. You should also post "deleteQuestions" method

    – Loading
    Nov 16 '18 at 14:01














  • 3





    What do you mean by it picks two?

    – tkausl
    Nov 16 '18 at 13:44











  • So I have a gameobject that contains 20 more sub-gameobjects these gameobjects are questions containing answers. Every time I press a button to activate a gameobject randomly from all the gameobjects that are disabled it sometimes activates two instead of one

    – BlindGuardian
    Nov 16 '18 at 13:50











  • You have a log statement in your CheckQuestion function. Does it indicate the function is being called twice? This is almost certainly the issue. Without knowing how the rest of your code dealing with the button works it's hard to give advice on how to prevent it from being triggered twice.

    – Retired Ninja
    Nov 16 '18 at 13:54











  • I have attached an Image so you can see what happens in the console.

    – BlindGuardian
    Nov 16 '18 at 13:58






  • 2





    I dont have a solution, but you should refactor your code. 1. Put all questions inside a list and iterate over the list instead of setting all the objects via hand. 2. CheckQuestions should return i instead of setting it globally. I guess the problem occurs that you dont disable the correct number somehow. You should also post "deleteQuestions" method

    – Loading
    Nov 16 '18 at 14:01








3




3





What do you mean by it picks two?

– tkausl
Nov 16 '18 at 13:44





What do you mean by it picks two?

– tkausl
Nov 16 '18 at 13:44













So I have a gameobject that contains 20 more sub-gameobjects these gameobjects are questions containing answers. Every time I press a button to activate a gameobject randomly from all the gameobjects that are disabled it sometimes activates two instead of one

– BlindGuardian
Nov 16 '18 at 13:50





So I have a gameobject that contains 20 more sub-gameobjects these gameobjects are questions containing answers. Every time I press a button to activate a gameobject randomly from all the gameobjects that are disabled it sometimes activates two instead of one

– BlindGuardian
Nov 16 '18 at 13:50













You have a log statement in your CheckQuestion function. Does it indicate the function is being called twice? This is almost certainly the issue. Without knowing how the rest of your code dealing with the button works it's hard to give advice on how to prevent it from being triggered twice.

– Retired Ninja
Nov 16 '18 at 13:54





You have a log statement in your CheckQuestion function. Does it indicate the function is being called twice? This is almost certainly the issue. Without knowing how the rest of your code dealing with the button works it's hard to give advice on how to prevent it from being triggered twice.

– Retired Ninja
Nov 16 '18 at 13:54













I have attached an Image so you can see what happens in the console.

– BlindGuardian
Nov 16 '18 at 13:58





I have attached an Image so you can see what happens in the console.

– BlindGuardian
Nov 16 '18 at 13:58




2




2





I dont have a solution, but you should refactor your code. 1. Put all questions inside a list and iterate over the list instead of setting all the objects via hand. 2. CheckQuestions should return i instead of setting it globally. I guess the problem occurs that you dont disable the correct number somehow. You should also post "deleteQuestions" method

– Loading
Nov 16 '18 at 14:01





I dont have a solution, but you should refactor your code. 1. Put all questions inside a list and iterate over the list instead of setting all the objects via hand. 2. CheckQuestions should return i instead of setting it globally. I guess the problem occurs that you dont disable the correct number somehow. You should also post "deleteQuestions" method

– Loading
Nov 16 '18 at 14:01












1 Answer
1






active

oldest

votes


















1














It would be nice to have more information about how this is structured, but based on what I see here, it looks like your WhatQuestion() method needs to know what the variable 'i' is. This is usually done by creating methods that accept parameters and return values. For this example, it looks like your CheckQuestion() method should return a value of 'i':



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


Then, your WhatQuestion() method should call CheckQuestion() to get 'i':



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


You may also need a way to de-activate all of the other questions so that only one is activated at a time. Something like



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


Then, to activate the one question:



QuestionList[i].SetActive(true);


Hope this info helps, it's my best guess with what's presented here.






share|improve this answer
























  • I just tried what you have suggested but unfortunately it is the same result

    – BlindGuardian
    Nov 16 '18 at 14:13












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339055%2frandom-range-is-picking-two-gameobjects-instead-of-one%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














It would be nice to have more information about how this is structured, but based on what I see here, it looks like your WhatQuestion() method needs to know what the variable 'i' is. This is usually done by creating methods that accept parameters and return values. For this example, it looks like your CheckQuestion() method should return a value of 'i':



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


Then, your WhatQuestion() method should call CheckQuestion() to get 'i':



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


You may also need a way to de-activate all of the other questions so that only one is activated at a time. Something like



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


Then, to activate the one question:



QuestionList[i].SetActive(true);


Hope this info helps, it's my best guess with what's presented here.






share|improve this answer
























  • I just tried what you have suggested but unfortunately it is the same result

    – BlindGuardian
    Nov 16 '18 at 14:13
















1














It would be nice to have more information about how this is structured, but based on what I see here, it looks like your WhatQuestion() method needs to know what the variable 'i' is. This is usually done by creating methods that accept parameters and return values. For this example, it looks like your CheckQuestion() method should return a value of 'i':



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


Then, your WhatQuestion() method should call CheckQuestion() to get 'i':



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


You may also need a way to de-activate all of the other questions so that only one is activated at a time. Something like



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


Then, to activate the one question:



QuestionList[i].SetActive(true);


Hope this info helps, it's my best guess with what's presented here.






share|improve this answer
























  • I just tried what you have suggested but unfortunately it is the same result

    – BlindGuardian
    Nov 16 '18 at 14:13














1












1








1







It would be nice to have more information about how this is structured, but based on what I see here, it looks like your WhatQuestion() method needs to know what the variable 'i' is. This is usually done by creating methods that accept parameters and return values. For this example, it looks like your CheckQuestion() method should return a value of 'i':



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


Then, your WhatQuestion() method should call CheckQuestion() to get 'i':



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


You may also need a way to de-activate all of the other questions so that only one is activated at a time. Something like



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


Then, to activate the one question:



QuestionList[i].SetActive(true);


Hope this info helps, it's my best guess with what's presented here.






share|improve this answer













It would be nice to have more information about how this is structured, but based on what I see here, it looks like your WhatQuestion() method needs to know what the variable 'i' is. This is usually done by creating methods that accept parameters and return values. For this example, it looks like your CheckQuestion() method should return a value of 'i':



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


Then, your WhatQuestion() method should call CheckQuestion() to get 'i':



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


You may also need a way to de-activate all of the other questions so that only one is activated at a time. Something like



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


Then, to activate the one question:



QuestionList[i].SetActive(true);


Hope this info helps, it's my best guess with what's presented here.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 14:06









Ben MulfordBen Mulford

415




415













  • I just tried what you have suggested but unfortunately it is the same result

    – BlindGuardian
    Nov 16 '18 at 14:13



















  • I just tried what you have suggested but unfortunately it is the same result

    – BlindGuardian
    Nov 16 '18 at 14:13

















I just tried what you have suggested but unfortunately it is the same result

– BlindGuardian
Nov 16 '18 at 14:13





I just tried what you have suggested but unfortunately it is the same result

– BlindGuardian
Nov 16 '18 at 14:13




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339055%2frandom-range-is-picking-two-gameobjects-instead-of-one%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine