GUI accepts input too quickly

I really hate posting my questions on these, I always feel someone out there has asked somewhere before, but I’m trying to make a code that uses numeric input to continue through some dialogue. You know the type, press 1 for this option, 2 for that, etc. Well, when the player hits the 1 button, it cycles through multiple entries, without letting you read or react to that which comes between. I’m not exactly the best Java programmer, but I don’t have the slightest idea on what I’m doing, or what the best course of action would be to prevent it. Here is the code, if it’s helpful.

function Update(){
	if (Welcome.start == true){
	guiText.text = "Welcome to the asylum, dear. \n 1. Where am I? \n 2. Who are you?";
	if (Input.GetKeyDown("1")){
	Welcome.start = false;
	guiText.text = "You are in the asylum. Bedlam. Mad ye' are, apparently. \n I know little about these things, I'm naught but reception. \n 1. When can I leave? \n 2. Who are you?";
		if (Input.GetKeyDown("1")){
		guiText.text = "I wouldn't know nothin' 'bout that. Doctors control that sort of thing, don't they?";
		}
	}
	}

}

you can’t nest if statements like that in the Update and get what you’re trying to do. The update is a single frame, there is no time delay between each if.

You’re going to have to add a little more structure I think. Maybe look into have a class/script which stores the information about the current step (i.e. a string for the text)and has an array of linked steps.

Ok. Is there any documentation on how to do this?

Look up coroutines; they make it easy to schedule a series of events.

–Eric

I was once told the best way of working out structure in OOP is to try and explain what you’re doing in a single sentence (or as few as possible) then look for the nouns…

There is to be a conversation with several stages, each stage can lead to another stage depending on user input.

objects
conversation; contains stages
stage; holds links to other stages, text etc.

I liked this as a challenge so I went a little further and built a prototype… I was thinking something a bit like the below. A manager object called Converstation which the user interacts with, and a data structure object to hold the stages. It’s just a quick example, but I hope it helps :smile:

Conversation.js

#pragma strict

var currentStage : Stage;
var startStage : Stage;

function Start()
{
	currentStage = startStage;
}

function OnGUI()
{
	if (GUI.Button(Rect(10,10,150,50),"Reset"))
	{
		Reset();
	}
	
	GUI.Label (Rect (10, 65, 1000, 20), currentStage.text);
	
	for(var i = 0; i<currentStage.options.length;i++)
	{
		if (GUI.Button(Rect(10,90+(i*55),150,50),currentStage.options[i].optionText))
		{
			ChangeStage(currentStage.options[i]);
		}
	}
}

function ChangeStage(pick : Stage)
{
	// handle whatever consequences of making the choice here
	currentStage = pick;
}

function Reset()
{
	currentStage = startStage;
}

Stage.js

#pragma strict

var text : String;
var optionText : String;
var options : Stage[];

pic of the hierarchy and inspector for one of the stages: Imgur: The magic of the Internet

Create an empty object, attach the Stage script and drag it back into the project to create a prefab template. Create an empty gameObject and attach the Conversation script, then pull the stage prefabs in as children (I renamed mine in the pic). Then you just need to populate the text and optionText fields, and the drag the stages into their options array to create the path.