Hello all, am having a problem when I want use the OnTriggerEnter method. So basically I have a list of information(questions) and I want to get a random question every time when the player enters the trigger, but I get spammed all the questions really fast instead. I didn’t manage to solve this problem so here is my code. I will be really thankful if somebody helps me.
public int width = 800;
public int height= 300;
public bool showWindow = false;
public bool gameOver = false;
public System.Collections.Generic.List <question> questions;
[Space(10)]
private playerController pc;
//private string countryName;
//private int score = 0;
private int randomNumber;
void Start () {
pc = GetComponent<playerController> ();
}
void OnGUI () {
/*if (Input.GetAxis ("Q") > 0) {
randomNumber = Random.Range (0, questions.Count);
showWindow = true;
}*/
if (showWindow) {
randomNumber = Random.Range (0, questions.Count);
GUI.Window (0, new Rect (Screen.width / 2 - width/2, Screen.height / 2 - height/2, width, height), WindowFunction, questions [randomNumber].questionName);
Screen.lockCursor = false;
pc.lockScreen = true;
}
}
private void OnTriggerEnter (Collider collider) {
//this.countryName = collider.gameObject.name;
if (collider.gameObject.name == "Trigger") {
showWindow = true;
}
}
private void OnTriggerExit (Collider collider) {
if (collider.gameObject.name == "Trigger") {
showWindow = false;
pc.lockScreen = false;
Screen.lockCursor = true;
}
}
private void rightAnswer () {
int rnd = Random.Range (1, 50);
int a = 0;
switch (questions[randomNumber].difficulty) {
case 1: a += 100 + rnd;
break;
case 2: a += 300 + rnd;
break;
case 3: a += 700 + rnd;
break;
}
pc.score += a;
showWindow = false;
pc.lockScreen = false;
Screen.lockCursor = true;
}
private void wrongAnswer () {
if (pc.lives > 0) {
pc.lives -= 1;
showWindow = false;
pc.lockScreen = false;
Screen.lockCursor = true;
}
if (pc.lives <= 0) {
Application.LoadLevel ("Game over");
gameOver = true;
}
}
private void WindowFunction (int windowID) {
if (GUI.Button (new Rect (50, 20, 600, 30), "A) " + questions [randomNumber].answerA)) {
if (questions [randomNumber].answerRight == 1) {
rightAnswer ();
} else {
wrongAnswer ();
}
}
if (GUI.Button (new Rect (50, 55, 600, 30), "B) " + questions [randomNumber].answerB)) {
if (questions [randomNumber].answerRight == 2) {
rightAnswer ();
} else {
wrongAnswer ();
}
}
if (GUI.Button (new Rect (50, 90, 600, 30), "C) " + questions [randomNumber].answerC)) {
if (questions [randomNumber].answerRight == 3) {
rightAnswer ();
} else {
wrongAnswer ();
}
}
if (GUI.Button (new Rect (50, 260, 600, 30), "Exit)) {
showWindow = false;
pc.lockScreen = false;
Screen.lockCursor = true;
}
}