I am currently facing an interesting issue. I have found that if you enable a script during an input even like KeyDown then the new script also executes the same event for the same frame.
So for example:
I have a controller script and 5 components each doing different things when you press enter. So I position my cursor over the option to enable script 2 and hit enter. It enables the component and the new component sees the key is down and loads component 3 which then does the same and before you know it I am right at the end of all my components with a single key press. (Stupid example, but you get what I mean)
So, to avoid this issue you simply add a yield before you enable the script. This way you skip a frame and the key down is no longer true when the component loads. Fine.
Now, my issue. I have a prefab that I instantiate with the enter button. Has to be the enter button. This prefab then removes control from the main game and takes over all keyboard functions and also does things on the Enter button being pressed. Now, sometimes, not always, but sometimes, when the prefab is instantiated, it immediately skips to the section after the first key press. I tried placing 2 yield statements before I instantiate the prefab and also have my Start function call an Init() function that also contains a yield.
I was under the impression that Start and Awake gets called before anything else but I am wondering if a Start calls a function that has a coroutine inside it, does this mean the OnGUI and Update functions wait before they start working or do they run concurrently?
Since the 2 yield statements before the instantiation doesn’t seem to work I replaced it with “yield WaitForSeconds(0.1)” but it still starts off with the enter key being pressed down some of the time…
Apart from building a timer into the prefab and testing at every single frame while the prefab is running wether the current time is > 0.001 seconds from the time the script started… What else can I do to prevent this glitch?
That makes sense and follows the documentation if the enabled component’s execution begins immediately and not a subsequent iteration of the game loop.
In your first example I would instead of relying on enabling and disabling components with input routines in them leading to the conflicts, use a small state machine to control which particular component has active input. Enumerate all the possible ‘objects’ you mention and change a tracking value on mouseOver instead of disabling/enabling components. Poof your input collision problems vanish.
if (GameManager.activeObject == enumComponent1) {
// process input
}
So now on to your current problem.
At first glance a yield should have done the trick. It suggests it’s something other than bypassing input on the same frame.
You’re not really clear on how your input polling is structured in the prefab you instance. Some more detail or code examples might help. We’ll certainly assume it’s not an oops moment like using GetKey instead of GetKeyDown!
You mention in passing a coroutine being launched by a function called by start. That raises an eyebrow.
You could try a more brute force sanity check that’s a little less convoluted than using timers and whatnot. That however is just addressing the symptom.
// Your prefab obviously
var falseInput = false;
function Start() {
if (Input.GetKeyDown ("enter"))
falseInput = true;
}
function Update() {
if (falseInput)
return;
// Poll input
}
function LateUpdate() {
if ((falseInput = true) (Input.GetKeyUp ("enter"))
falseInput = false;
}
I used to be a big fan of GetKeyDown but some or other event that I don’t recall any more changed my mind to using GetKeyUp instead so I use that one for both…
I was actually thinking of instantiating my prefab during the GetKeyUp and having the prefab use GetKeyDown for it’s input… The idea just hit me now… Can’t believe I didn’t think of it before… Oh, no wait…
Oh my goodness, wait a tad… I think you hit the nail on the head!!! I think my issue was an oops moment… I instantiate the prefab on KeyDown and the prefab uses only KeyUp for it’s input… So all the yield statements in the world won’t make any difference if I keep the button down long enough…
I actually started this reply saying I liked the falseInput idea you mentioned. Although having to add an extra test for every Update call is a bit of a waste I reckon one function call out of the possible hundred millions per second isn’t THAT big of a deal… but in my reply I hit the actual error and I think I now know how to continue from here…
I’f this solution of mine doesn’t work I’ll get back to you. Thanks for this
P.s. I was actually stuck on that issue for a while and ended up doing the falseInput thing (although I called mine firstPass) to overcome that issue. It worked wonderfully but at some point I had to scrap the entire project and start again from scratch… At this point I actually started using the enumeration thing and that is what I am still using.
The deal is, this is all input for various states of the same thing… The thing I am doing now is a completely standalone thing that needs to start, disable everything else and then be in complete control. Since it could start at any time and through numerous methods, the enumeration thing didn’t do the trick. Since the enumeration thing worked so well I had actually completely forgotten about my firstPass method until you brought it up again…
Nice to know my hacks are being used by others also… that it doesn’t make me a poor programmer
I hope this keyup / keydown thing is going to fix my current issue, though… only 7 more hours till I can go home and check…