Why Do I keep Getting An Infinite Loop? Speech Dialogue
Hi Everyone, I am trying to use the legacy textfield function to do dialogue in my 3D game. I have the npc set to Trigger, as I collide with it, but I am trying to have a Do While Loop where when I press the F key, it goes to the next text. But I assume that it is not working because the talkcount++ is in an if statement and therefore goes into an infinite loop because it cannot increment the value of talkcount unless that is called. Please help me understand how I can Fix this or if there is a better way of doing Speech Dialogue that I do not know of. Thanks for your help! SwipeFastStudios
using UnityEngine; using System.Collections; public class Player : MonoBehaviour {
bool mousedown = false;
bool talk = false;
bool next = false;
int talkcount = 0;
string[] talking = {"Hello, My Name is Prof. Dungbeatle", "You are now in the Realm of Adventure", "test"};
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnMouseDown(){
mousedown = true;
}
void OnTriggerEnter(Collider other){
if (other.name == "Capsule") {
if (mousedown == true){
Destroy(other.gameObject);
}
}
if (other.name == "npc") {
talk = true;
}
}
void OnGUI(){
if (talk == true){
GUI.enabled = true;
do {
GUI.TextField (new Rect(300,10,400,200),talking[talkcount]);
if (Input.GetKeyDown(KeyCode.F)){
talkcount++;
}
}while ( talkcount < talking.Length);
}
}
}
you can eli$$anonymous$$ate the use of the loop by transferring the key check to update. increase talkcount there. in OnGUI ins$$anonymous$$d of toggling a bool, return if talkcount is greater then its length.
Your answer
Follow this Question
Related Questions
Can´t find the infinite loop in this script 0 Answers
How to stop this infinite loop 1 Answer
Respawn Script Is Not Functioning 0 Answers
Photon VS Error 0 Answers
Potential infinite while loop? 1 Answer