I wrote a code for an object that instantiates by pressing the space bar. Now, I also wanted it to appear only every ten seconds. So, I wrote this code-
public class Player : MonoBehaviour
{
[SerializeField]
private GameObject _laserPrefab;
[SerializeField]
private float _fireRate=10.0f;
private float nextFire=0f;
// Start is called before the first frame update
void Start()
{
transform.position=new Vector3(0,0,0);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && (Time.time>nextFire)){
nextFire=Time.time+_fireRate;
Instantiate(_laserPrefab,transform.position+new Vector3(0,1f,0),Quaternion.identity);
}
}
This code is instantiating the object every time space bar is pressed, but it is not giving a gap of 10 seconds, which I want.