All I want to do is add a delay to some execution of code.
I already have a couple of delays when the player gets hit. However, I can’t seem to make the enemy delay from spawning. The enemy has some pretty basic code, but I can’t seem to get the
IEnumerator function()
and
yield return new WaitForSeconds(1.5f);
to work at all. Basically the “wait for X seconds” is all I’m really looking to do.
edit
Ok, here is my entire “blocker” code. (blocker is the enemy here)
`
using UnityEngine;
using System.Collections;
public class Blocker : MonoBehaviour
{
#region Variables
public float MinSpeed;
public float MaxSpeed;
public float currentSpeed;
private float x, y, z;
#endregion
#region Properties
#endregion
#region Functions
void Start()
{
SetPositionAndSpeed();
}
void Update()
{
float amtToMove = currentSpeed * Time.deltaTime;
transform.Translate(Vector3.down * amtToMove);
if (transform.position.y <= -5)
{
SetPositionAndSpeed();
}
}
public void SetPositionAndSpeed()
{
currentSpeed = Random.RandomRange(MinSpeed, MaxSpeed);
x = Random.RandomRange(-3.7f, 4.1f);
y = 8.0f;
z = 0.0f;
transform.position = new Vector3(x, y, z);
}
void OnTriggerEnter(Collider otherObject)
{
//if blocker hits the player, delay the blocker's spawn
if (otherObject.tag == "player")
{
StartCoroutine(BlockWait());
}
}
IEnumerator BlockWait()
{
yield return new WaitForSeconds(1.5f);
}
#endregion
}
`