- Home /
How to "fake" a button press through code?
I think the question is pretty self explanatory, I usually give details as to my specific situation but I'm not sure I could explain it without just making things overcomplicated. Basically, I want a line of code that could "simulate" a button press. As in, for example, when the game starts I want Unity to think that the player just pushed the left alt key, but he actually didn't. It would be hard to explain why I want this, but really there is no other way to achieve what I am looking for. If anybody knows of any code that could do this, or any way at all to trick Unity into thinking the player pushed a button, I would appreciate it very much. Thank you in advance for any help.
Answer by LMan · May 24, 2014 at 07:20 PM
Well I'm thinking that maybe you could make the instructions that get executed on button press into a method, and then call that method where you want to fake a button press.
Or you could have the button press trigger a bool, which triggers the code- then to fake the button press you would simply toggle the bool.
The info you get straight from Input is strictly read-only as far as I know, but there are many other ways that you can trigger code to execute.
Haha alrighty, I suppose that's the only way to do it then. The problem here is that my code is very complicated to do this with, so I think I'll have to go reformat a bunch of my code. I tried to avoid it... oh well, it'll probably be good anyway. Thanks you guys!
Aw that sucks, I was afraid of that. Oh well, glad to be of help! Good luck!