x


OnGUI: += and -= operations problem

So I'm having an issue that I didn't have in the previous version of Unity3d. I have a variable that I am using the += and +- operation on every time the user presses down on a specific key. All I want to do is add the value 1 but every time the key is pressed the variable gets the value 2 added/subtracted to it. I have debugged for Input.GetKeyUp() and Input.GetKeyDown() and both times the debug shows 2 outputs in the console. I'm not too sure what's going on. I have gone around the problem by just using a float and halfing everything but its a pain in the you know what. If anyone has a solution please help!

Thanks, Austin

more ▼

asked Oct 09 '10 at 12:37 AM

Austin 3 gravatar image

Austin 3
28 6 6 11

That will have happened in 2.6 as well, my guess is you were just lucky with the number of events matching up each time

Oct 09 '10 at 02:02 AM Mike 3
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

The OnGUI() function is called more than once per frame (at a minimum of twice), so if GetKeyUp() and GetKeyDown() are inside OnGUI(), this would cause the problem you are experiencing. Putting GetKeyUp() and GetKeyDown() inside Update() should fix your problem.

more ▼

answered Oct 09 '10 at 12:56 AM

PeterDC gravatar image

PeterDC
277 2 4 14

(comments are locked)
10|3000 characters needed characters left

Perhaps the script is accidentally on more than one object?

Sometimes it's not obvious what object(s) a script is on. An easy way to find them is to put a line of code in the script:

Debug.Log("My name is: " + gameObject.name);

You could put this next to the increment/decrement code to see who is calling it.

more ▼

answered Oct 09 '10 at 01:34 AM

Bampf gravatar image

Bampf
5k 8 19 49

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x789
x244
x40
x37
x8

asked: Oct 09 '10 at 12:37 AM

Seen: 903 times

Last Updated: Oct 09 '10 at 12:37 AM