0
\$\begingroup\$

Hi I'm trying to get NGUI's HUDtext to display a set of text message one of which being "Try using WASD to move" and displaying "Well done!" upon input of any of the four keys. Unfortunately I'm not well versed in C# and am currently stuck and unsure how to implement this.

using UnityEngine;
using System.Collections;
using System;

//public KeyCode KeyCode.Alpha2;

public class Test : MonoBehaviour {

    public int seen = 123;
    public HUDText hudText;

    void Start() {

        StartCoroutine(MyCoroutine());
        //Debug.Log(KeyCode);
        Tutorial ();

    }


    void Tutorial() {

        while (true) {
            while (!Input.GetKeyDown(KeyCode.Return)) Debug.Log ("Hit return");
        }

    }

    IEnumerator MyCoroutine() {

        hudText.Add("Welcome", Color.white, 3f);
        yield return new WaitForSeconds(3);    //Wait one frame

        hudText.Add("This is System Defence", Color.white, 4f);
        yield return new WaitForSeconds(4);

        hudText.Add ("Try using WASD to move", Color.red, 3f);
        yield return new WaitForSeconds(3);

    }

}

I would appreciate it if someone could tell me how would I go about doing this.

\$\endgroup\$
3
\$\begingroup\$

Considering you have a while(true) loop in your code, you're not going to allow the game loop to move to the next frame. You'll need to move to the next frame to update the screen and gather new input.

You can use the void Update() method to check for stuff like keyboard input every frame.

void Update() {
    if(Input.GetKeyDown(KeyCode.W) 
     | Input.GetKeyDown(KeyCode.A)
     | Input.GetKeyDown(KeyCode.S)
     | Input.GetKeyDown(KeyCode.D)) {
         hudText.Add ("Nice work! Now try...", Color.red, 3f);
     }
}

Obviously you'll also want to keep track of the state of the users progress, so you don't keep showing the same messages over and over. But, the Update() method is the place for those types of things.

\$\endgroup\$
  • \$\begingroup\$ Thanks for the reply. I tried calling a public boolean variable and setting to false. I then nested the if chunk above in a while statement in which the boolean will switch from false to true once any of the 4 keys are pressed. However unity simply hangs on start. Any idea why? Thanks. pastebin.com/KKJ7hxFJ (Pastebin script, if my explanation was not clear enough) EDIT : An "if" statement works and doesn't crash unity. Still curious to know why though. \$\endgroup\$ – user4985 Aug 4 '14 at 16:08
  • 1
    \$\begingroup\$ You've once again put a while(true) statement in your code. That's what while (trigger == false) evaluates to. It's hanging because as I said, you're not allowing the game to move to next frame. I suggest you study a bit more about how loops work before using them. Take the while loop out of your code. \$\endgroup\$ – MichaelHouse Aug 4 '14 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.