I’ve made a dialogue box that displays the text. Now I’m trying to figure out how to make my script wait for the player to press the Space Bar and then the code will continue running sequentially. I’ve tried using while loops and Update(), but neither will work. How can I make the game wait until the player presses the Space Bar? I’m new to C# and Unity so any help would be greatly appreciated! Thanks!
My code so far (Sorry that it’s so messy.):
This is what changes the text and waits for input:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TextDisplayer : MonoBehaviour
{
public GameObject dialogBox;
public Text speechText;
public void Say(string whatTextToDisplay)
{
speechText.text = whatTextToDisplay;
while (!Input.GetKey ("space"))
}
}
And this is where I test it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SayHello : MonoBehaviour
{
public GameObject dialogBox;
public GameObject thisGameObject;
public TextDisplayer myTextDisplayer;
void Update()
{
dialogBox.SetActive (true);
myTextDisplayer.Say ("Hello!");
dialogBox.SetActive (false);
}
}
And any feedback on how I can rewrite my code would be appreciated, as well! Thanks!
You can stop the Update-function, by enabling and disabling a script, for example dialogBox.enabled = false;
. But you can also use booleans that surround the content of the Update-funtion. For example:
public class TextDisplayer : MonoBehaviour
{
public ObjectYouWantToPause obj;
void Update()
{
if(!input.GetKey ("space"))
{
obj.pause = false;
}
}
}
public class ObjectYouWantToPause : MonoBehaviour
{
public bool pause = true;
void Update()
{
if(!pause)
{
...
}
}
}
@Destolos Let me rephrase my question. How would I make the program wait for the space bar to be pressed and then go onto the next line of text? I’m still having trouble and don’t really know what to do. I’m used to BASIC where each line is executed one after the other in one script. I’m also not used to Update() and Start(). I know what they do, they are just a bit trickier than what I’m used to. Any suggestions on how I should go about rewriting and setting up my code. Should I use a coroutine?
Try the following and tell me if it works. I am just at work and can’t actually test it.
int blockNum = 0;
bool canRun = false;
void Update () {
if (Input.GetKeyUp)
canRun = true;
if (!canRun)
return;
switch (blockNum ) {
case 1:
CodeBlockOne ();
canRun = false;
break;
case 2:
CodeBlockTwo ();
canRun = false;
break;
blockNum++;
}
}
void CodeBlockOne () {
Debug.Log ("Code Block One");
}
void CodeBlockTwo () {
Debug.Log ("Code Block Two");
}