To Key Presses problem

Please

Hello.
I have problem with my code…Is not working with Utity.Please what can I do?
Thank you…

When you say the problem, do you mean that:

When you press the up arrow, the message “Up arrow pressed” doesn’t appear at the bottom of your screen?

If so, can you confirm you have dragged the NumberWizard script onto a game object in the game?

I am meaning when I write
if (Input.GetKeyDown(KeyCode.UpArrow)) {
print(“Up arrow pressed”);
}

	if (Input.GetKeyDown(KeyCode.DownArrow)) {
		   print("Down arrow pressed");

in MonoDevelop: and save it after click in Unity nothing appear in Console section…

Your code looks fine, might be worth trying a couple of things:

Open your ‘NumberWizards’ script and ‘build’ it again (it might be ctrl + B on your windows machine.

Might even be worth making a new script, another game object, re-write the code into that new script and playing that.

It might also be worth maybe restarting Unity again if that doesn’t work.

Apart from that, I can’t immediately see a problem with your code :S

Thank you…I was try built again but nothing happen…I think that I need re-write the code and will see.

You might have to click in the game window to give it focus before pressing any buttons.

I re-write my code and still same…I don’t know what can I do to next?

Hi @Libor_Fajdek,

It would be handy to rule a few things out…

Could you, in your NumberWizards.cs script change the following line;

int max = 1000;

to;

int max = 2000;

please, and then take another screenshot of the game running, with the output in the console window.

Hi Rob.
I change it but still same…Not Key working…

1 Like

Hi,

Ok, but we got some success from this - by changing that value and running the game we saw that 2000 appeared in the console - so now we definitely know that this script is attached to the NumerWizard game object and is the one running.

Could you copy and paste your code into a reply to this post please @Libor_Fajdek, not a screenshot, but a copy and paste. I would like to test for myself your current code but within a new project at this end.

The reason I suggest this is that I note you have multiple projects within the solution on the left of your original screenshot - I’d like to start with a blank canvas so to speak and see what happens.

Also, can you confirm which version of Unity you are using please - I will download the same version for test purposes.

There’s a hidden warning in your console. What’s that warning?

(Click on the little yellow warning symbol in the corner of the console to see it.)

1 Like
using UnityEngine;
using System.Collections;

public class NumberWizards : MonoBehaviour {

	// Use this for initialization
	void Start () {
	   print ("Welcome to Number Wizard");
	   print ("Pick a number in your head but, don't tell me!");
	   
	   int max = 2000;
	   int min = 1;
	   
	   print ("The highest number you can pick is "  + max);
	   print ("The lowest number you can pick it "  + min);
	   
	   print ("Is the number higher or lower than 500?");
	   print ("Up = higher, down = lower, return = equal");
	}
	
	// Update is called once per frame
	void Update () {
	
		if (Input.GetKeyDown(KeyCode.UpArrow)) {
			  print("Up arrow pressed");
		}
		
		if (Input.GetKeyDown(KeyCode.DownArrow)) {
			  print("Down arrow pressed");
		}	
	}
}

my Unity Version is 4.6.9f1

This appear on beginning…The referenced script on this Behaviour is missing!

Click on the NumberWizard GameObject in the Hierarchy, and then with the Inspector visible, can you provide another screenshot please.

It sounds like you haven’t got the script attached, but then it’s a bit odd that the code change above ran - I suspect it’s from one of the other projects you have under the same solution but if you can pop another screenshot up that would help.


Updated Tue Mar 28 2017 20:05

The code is fine…

So lets have the screenshot next and we’ll look at that…

Hi…This is my another screenhot.

1 Like

Hi @Libor_Fajdek,

Ok, great - this is really useful.

You see in the Inspector you currently have two Script components.

One (marked with the red 1) just says (Script) because the file is missing, most likely deleted and recreated.
The second (marked with a red 2) is called Number Wizards (Script), which is your current file.


  • On the first script component, where I have indicated in the following image with the red square box, click there and then select “Remove Component”

  • Leave the second script as it is.

  • Click on the Run button but make sure you are on the Game window

  • Once on the Game window, click inside it (indicated by the large red box)

  • Press the Up Arrow

  • Press the Down Arrow

  • Tell us that it is now working and all resolved :wink:

1 Like

Thank you so much…It’s working now…Great…I am glad for that…Thank you one more time…

1 Like

Good job!

I have been following this thread for ages as i really wanted to know why it wasn’t working! (Just needed to know!)

Glad it’s all sorted :grin:

1 Like

You are more than welcome @Libor_Fajdek, sorry it took a little while, it’s sometimes a little bit challenging doing these things remotely and you have to get all the bits of the puzzle together :slight_smile:

Hopefully it has been useful for you also and you will know what things to keep an eye out for moving forwards.