I have pretty much started c# and unity a few weeks ago and so far I could make a lot of stuff, but currently I´m stuck with the problem:
I wanted to spawn a cube when I press X.
However, I can spam X and therefore create that many cubes that unity starts lagging.
I wanted to build some kind of delay that prevents the button spamming.
using UnityEngine;
using System.Collections;
public class SpawnTest : MonoBehaviour
{
public Vector3 ObjectSpawnPosition;
public GameObject obj;
public float SpawnRate = 1F;
private float DelayTimer = 0F;
void Update ()
{
if (Input.GetKeyDown(KeyCode.X) && Time.time > DelayTimer)
{
Instantiate (obj, ObjectSpawnPosition, Quaternion.identity);
DelayTimer -= Time.time + SpawnRate;
}
}
}
I did read a lot of stuff about Invoke, Couroutine, WaitForSeconds, IEnumerator… but everything seems not to solve this issue, or I´m just to dumb to understand -.-
sorry for my bad english, I hope someone can help to solve this or better to understand this problem.
A additional problem is that I create to many “clones” of the object. Is there any way to prevent this from happening?