Question by
Masonic321 · Dec 23, 2018 at 02:34 PM ·
inputupdateupdate functionwait
How to make a script wait for an input
Hi guys Here is my script
public GameObject buildingFloor;
public GameObject buildingWall;
public BuildMode checkIfBuildModeActive;
public UIManager uiManagerScript;
public Transform greenFloor;
public Transform greenWall;
public GameObject greenFloorGO;
public GameObject greenWallGO;
// Use this for initialization
public void Start ()
{
greenFloorGO.SetActive(false);
greenWallGO.SetActive(false);
}
// Update is called once per frame
public void Update()
{
if (uiManagerScript.woodAmount >= 300)
{
if (checkIfBuildModeActive.buildModeActive == true)
{
if (Input.GetKeyDown(KeyCode.Q))
{
greenFloorGO.SetActive(true);
if (Input.GetButtonDown("Fire2"))
{
Instantiate(buildingFloor, greenFloor.position, greenFloor.rotation);
uiManagerScript.woodAmount -= 300;
greenFloorGO.SetActive(false);
}
}
if (Input.GetKeyDown(KeyCode.E))
{
greenWallGO.SetActive(true);
if (Input.GetButtonDown("Fire1"))
{
Instantiate(buildingWall, greenWall.position, greenWall.rotation);
uiManagerScript.woodAmount -= 300;
greenWallGO.SetActive(false);
}
}
}
}
}
I need to make it wait for the Fire1 and Fire2 Input, because i currently have to press them straight after i press q or e please help!!!
Comment
Answer by Vega4Life · Dec 23, 2018 at 03:58 PM
Here is an idea using a container for the key and action after pressing it. I am sure it can be improved, it was just what I was thinking at the time. I hope it helps.
using UnityEngine;
using System;
public class InputWait : MonoBehaviour
{
ButtonAction buttonAction;
void Update()
{
if (buttonAction == null)
{
if (Input.GetKeyDown(KeyCode.Q))
{
buttonAction = new ButtonAction("Fire2", BuildFloor);
}
if (Input.GetKeyDown(KeyCode.E))
{
buttonAction = new ButtonAction("Fire1", BuildWall);
}
}
else if (buttonAction.CheckWaitKey())
{
buttonAction = null;
}
}
void BuildFloor()
{
Debug.Log("Build floor things");
}
void BuildWall()
{
Debug.Log("Build wall things");
}
}
// Just a container class to hold the key and the callback
public class ButtonAction
{
public string input;
public Action callback;
public ButtonAction(string input, Action callback)
{
this.input = input;
this.callback = callback;
}
public bool CheckWaitKey()
{
if (Input.GetButtonDown(input) && callback != null)
{
callback();
return true;
}
return false;
}
}