- Home /
Question by
DubstepDragon · Jul 25, 2013 at 10:36 AM ·
updatespawnconvertconversioninvokerepeating
Convert InvokeRepeating into the Update method...
I want to convert the InvokeRepeating section of this script, into spawning via the Update() method. Can anyone help me? Thanks in advance!
Here is my script:
using UnityEngine;
using System.Collections;
public class Cubes : MonoBehaviour {
public float delay;
private float lastIncreased;
private float timeDelay = 10;
public GameObject cube;
void Start () {
InvokeRepeating("Spawn", delay, delay);
}
void Spawn () {
Instantiate(cube, new Vector3(Random.Range(-7.3f, 7.3f), 10, 0), Quaternion.identity);
}
void Update() {
if(Time.time > timeDelay + lastIncreased) {
lastIncreased = Time.time;
delay -= 0.05f;
}
}
}
:D
Comment
Best Answer
Answer by robertbu · Jul 25, 2013 at 01:17 PM
using UnityEngine;
using System.Collections;
public class Cubes : MonoBehaviour {
public float timeDelay = 10.0f;
private float timestamp;
public GameObject cube;
void Start () {
timestamp = Time.time + timeDelay;
}
void Update() {
if(timestamp < Time.time) {
Instantiate(cube, new Vector3(Random.Range(-7.3f, 7.3f), 10, 0), Quaternion.identity);
timestamp += timeDelay;
}
}
}