- Home /
Xbox controller not working in OnGUI but, otherwise it does?
Hi.
I am using a 360 controller to control my game and it works well but, I noticed that the controller dose not respond to code inside the OnGUI function. How can this be? My controller works fine outside OnGUI(tested with prints) but, as soon as I access the OnGUI function the controller wont respond to something like :
function OnGUI (){
if(Input.GetButtonDown("XButton")){
print("Moving")
}
}
Yes the Input buttons are setup correctly as said the controller responds fine from Update, LateUpdate, FixedUpdate.... I just dont get why it wont respond in OnGUI when it responds in all the other functions.
Can you please help me? Since my game is a GUI-heavy game it is really a setback not to be able too use my 360 controller.
Or is this a limitation of Unity? That you cannot use a 360 controller in a GUI function?
Thank you.
Answer by DevonJavaScript · Aug 27, 2011 at 09:44 AM
2 Possible ideas. 1)Try taking the input variable outside say into a function Update() and use the gui highlight or when over with cursor allow the input to be done. 2) Make a Script souly for the Guis with a cycling for when its this certain number change texture and when over that number and certain button hit goto place - Example:
var numberOfOptions = 3; var currentNumber = 1; var gui1: Transform; var gui2: Transform; var gui3: Transform; var highlightTexture: Texture; var normalTexture: Texture; private var texture1; private var texture2; private var texture3; function Update() { texture1 = gui1.GetComponent(GUITexture).Texture; texture2 = gui2.GetComponent(GUITexture).Texture; texture3 = gui3.GetComponent(GUITexture).Texture; if(Input.GetButtonDown("UpKey")) { private var stop = 0; if(currentNumber == numberOfOptions) { stop = 1; currentNumber = 1; } if(currentNumber < numberOfOptions) { if(stop == 0) { currentNumber = currentNumber + 1; } stop = 0; } } if(Input.GetButtonDown("DownKey")) { private var stop = 0; if(currentNumber == 1 ) { stop = 1; currentNumber = numberOfOptions; } if(currentNumber > 1) { if(stop == 0) { currentNumber = currentNumber - 1; } stop = 0; } } if(currentNumber == 1) { texture1 = highlightTexture; if(Input.GetButtonDown("XButton")) { //goto code }
} else { texture1 = normalTexture; } if(currentNumber == 2) { texture2 = highlightTexture; if(Input.GetButtonDown("XButton")) { //goto code }
} else { texture2 = normalTexture; } if(currentNumber == 3) { texture3 = highlightTexture; if(Input.GetButtonDown("XButton")) { //goto code }
} else { texture3 = normalTexture; } }
Its not on GUI but its simulating it it is good for console only. sorry if i wasn't a help. Glad if I did help.
Answer by Joshua · Aug 27, 2011 at 12:10 PM
OnGUI gets called several times per frame, so input doesnt work. Use event.current instead. I'd post a link to the docs but im on my phone ;)
input works for me as long as you edit the input macros and make it joystick variables in the input. Its simple and works. I have gotten it to work multipul times.
Sure, it works, but it doesn't work properly. Say when you click up on your controller it scrolls up in the gui, if you use Input for this up will return true for a single frame. Since OnGUI can get called several times per frame you will be scrolling up several times for each click.
Since stuff like this is obviously never what you want you should just use Event.current, which is designed to work for exactly this. Why would you bother using something that isn't?
Your answer
Follow this Question
Related Questions
How to set the Ongui's GUILayout.BeginArea in a panel??? 0 Answers
How to get an array inside a simple class at Editor window and change arraysize? 0 Answers
How to get individual trigger input using Xbox 360 controller? 3 Answers
How to stop controller vibration on minimize 1 Answer
Help Needed: How to Use Input Manager on UI Button 0 Answers