- Home /
How to script in a button press to continue?
How do I script it (in a two player game where both players alternate using the mouse for one click at a time) so that after the first player clicks, the second player can't click until he or she presses a button on the keyboard, and then after he clicks, the first player has to press another button on the keyboard.
How can I do this without resorting to loading separate levels each time a player clicks? I would prefer them to see the screen at all times.
Answer by jrfactotum · Oct 21, 2010 at 10:18 PM
I imagine you could do something similar to this (in a C# MonoBehaviour-derived class):
bool clicked = false; bool pressedButton = false;
void Update() { if (Input.GetMouseButtonUp(0)) clicked = true;
if (clicked && Input.GetKeyDown(GetPlayerButtonName())) pressedButton = true;
if (clicked && pressedButton) { clicked = false; pressedButton = false; SwitchPlayer(); } }
GetPlayerButtonName()
would return the name of the keyboard button that the other player is required to press after the current player clicks.
Hope this helps.
Okay, so I have some questions. First of all, I'm very new to scripting and I have been using JavaScript. I think I converted it fine, but how do I work with the GetPLayerButtonName() and SwitchPlayer(); I don't really understand where these come from and how I implement them into the script. Like do I define these things elsewhere, and what would I do to define them?
Those are functions you'll have to write yourself. Sounds like you would benefit from an introduction to general Unity scripting:
http://unity3d.com/support/documentation/ScriptReference/index.html
http://unity3d.com/support/documentation/$$anonymous$$anual/Scripting.html
I'd hate to give you all the answers and deprive you of the benefit of figuring out how to implement simple functions, so I'll leave implementing those functions as an exercise for you.
Actually, I will give you a hint about how to implement GetPlayerButtonName(). If the player is player 1 ('player' could be a member variable), return player 1's button name. If player 2, return player 2's button name. Input names for buttons, mice and other input devices are defined in Edit->Project Settings->Input. See here: http://unity3d.com/support/documentation/$$anonymous$$anual/Input.html
Alright, thanks! Yeah, I just need a push in the right direction, I'll get on it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613080345im_/https://answers.unity.com/themes/thub/images/avi.jpg)