I have an AI responding to me when I type something in, which works without error. Except I can type the same thing in any number of times, and it will respond the same thing all those times (which I do not want). Therefore, I want it to recognize when a question has been asked.
I made a variable “saidHello=false” and required the AI to answer only if saidHello is false. Once I’ve typed in ‘hello’, saidHello should become true. PROBLEM: when saidHello becomes true, the AI does not display the answer anymore! I want it not to display the same answer only after it’s been asked once, not once it’s been asked once. I’ve been struggling to find a solution to this.
So I have provided a script. This script will display certain texts based on the keys that are pressed. Its basically the same as what you are trying to do. However, this script prevents the system from displaying the same message.
var answeredQuestion1 : boolean = false;
var answeredQuestion2 : boolean = false;
var answeredQuestion3 : boolean = false;
var questionNumber : int = 0;
var questionAnswer : String;
function OnGUI()
{
switch(true)
{
case questionNumber == 1 && !answeredQuestion1:
// Prevents the AI from answering the same old question.
answeredQuestion1 = true;
Debug.Log("Q1");
break;
case questionNumber == 2 && !answeredQuestion2:
answeredQuestion2 = true;
Debug.Log("Q2");
break;
case questionNumber == 3 && !answeredQuestion3:
answeredQuestion3 = true;
Debug.Log("Q3");
break;
}
// Displays the answer.
GUI.Label(Rect(100, 100, 100, 100), questionAnswer);
}
function Update()
{
// Instead of typing something in, you have to press a key.
if(Input.GetKeyDown("1")) {
// The question flag.
questionNumber = 1;
// The answer.
questionAnswer = "Question 1 answered.";
}
else if(Input.GetKeyDown("2")) {
questionNumber = 2;
questionAnswer = "Question 2 answered.";
}
else if(Input.GetKeyDown("3")) {
questionNumber = 3;
questionAnswer = "Question 3 answered.";
}
};
So if I pressed the number 1 key, it will display “Question 1 answered.” once.