[Solved]NumberWizzard - How to print different messages using the same KeyCode?

Instead of displaying all print messages at once when the game starts, I want the user to receive new messages by pressing KeyCode.Space.

Observed

When KeyCode.Space is pressed all the messages appear at once.

Desired

With every GetKeyDown (KeyCode.Space) by the user, a new message to appear.

Steps to reproduce

create a list of print messages which to be displayed when a specific key is pressed

Relevant information

Unity 4.7.2f1

The Code:
https://gist.github.com/DragoshCraciun/bd336c845f733ce4619bde9cd76aad9d

Steps tried already

  1. using if statements
  2. using if else statements
  3. using variables
  4. using strings
1 Like

the problem is that when you use many ifs like you have done, it will pass true to all of them at the same time, i think that a better approach would be to put a controller to the message that will be displayed such as:

void MsgController ()
{
switch (message)
{
case 0: print (“x”);break;
case 1: print (“x2”); break;
case2: print (“x3”); breal;

etc…
}
}

void Update()
{
if (Input.GetKeyDown (KeyCode.Space) && message <= /* number of cases */)
{
MsgController();
message++
}

else if (Input.GetKeyDown (KeyCode.Space) && message > /* number of cases */)
{
// put something here to continue your game or to control the message number in order to choose what will be displayed to the player.
}
}

haven’t tried it, but it might work =x let me know

1 Like

Did it worked?

Thank you very much for your help Joao_Dalvi,

I just finish writing the code and now it works beautifully! :slight_smile:
Please take a look and tell me what you think:

https://gist.github.com/DragoshCraciun/ba43d4cfe978f2cb71eae361d65d923e

Also the entire code to the game can be found here:

https://gist.github.com/DragoshCraciun/e0089a905c7f91fd09f66ed195018cbe

1 Like

I am happy to see that this issue was solved =D

The code is looking good! Congrats!
Would you mind changing the [Help] in the title to [Solved]?

Privacy & Terms