- Home /
The question is answered, right answer was accepted
Dialogue with GetKey - Codes
Hello everyone, I'm new to programming but have been able to make a basic demonstration for a game that I am now trying to flesh out a bit. But I keep running into a problem. I tried making a dialog system that ended up looking a bit like this:
public float conversation;
void Update(){
if (Input.GetKeyDown(KeyCode.Space)){
panel.SetActive(true);
text.text="Hello.";
conversation=1;
}
if (conversation==1 && Input.GetKeyDown(KeyCode.Space)){
conversation=2;
text.text="What's up?";
}
if (conversation==2 && Input.GetKeyDown(KeyCode.Space)){
panel.SetActive(false);
}
Basically what happens is I run the script (the trigger works fine so far) and it just skips through the entire dialog to the end. I've tried using .GetKeyDown, .GetKeyUp... and even having a float that would count down (countdown-= Time.deltaTime) and even to only read this part of the script if it is countdown<=0. None of these solutions has worked.
If anyone has ideas, please feel free to share because I'm at a bit of a loss.
Answer by Glurth · Jul 17, 2018 at 04:11 PM
There is a single keyword you need to fix this: "else"
Notice that if the first condition is true- you MAKE the second condition true (and so on).
So, if you just add an else
before if (conversation==1..
(and all those that follow), it will ensure that only ONE dialog option get executed per call to Update()
Alternatively: you could use the 'return' keyword, at the end of each dialog option. But this assumes there is nothing else in update you need to execute lower down. Also, take a look at the c# case..switch command, perfect for this sort of thing!
Thank you so much! It worked like a charm... It was a bit embarrassing, having all sorts of things run perfectly fine but no functioning dialog system in a story heavy game.