Can i add a time var to my spawnInterval for increasing spawing over time?
Is there a way to "just" add time as a variable to my spawnInterval script? Im totally new to c#/unity and tried to modify the project playground scripts, but I can't find the way to do it. This is the script I'm using:
using UnityEngine; using System.Collections;
[RequireComponent(typeof(BoxCollider2D))] public class AutoObjectSpawner : MonoBehaviour
{ [Header("Object creation")]
// The object to spawn
// WARNING: take if from the Project panel, NOT the Scene/Hierarchy!
public GameObject prefabToSpawn;
public static float timeSinceLevelLoad;
[Header("Other options")]
// Configure the spawning pattern
public float spawnInterval = 1;
private BoxCollider2D boxCollider2D;
void Start ()
{
boxCollider2D = GetComponent<BoxCollider2D>();
StartCoroutine(SpawnObject());
}
// This will spawn an object, and then wait some time, then spawn another...
IEnumerator SpawnObject ()
{
while(true)
{
// Create some random numbers
float randomX = Random.Range (-boxCollider2D.size.x, boxCollider2D.size.x) *.5f;
float randomY = Random.Range (-boxCollider2D.size.y, boxCollider2D.size.y) *.5f;
// Generate the new object
GameObject newObject = Instantiate<GameObject>(prefabToSpawn);
newObject.transform.position = new Vector2(randomX + this.transform.position.x, randomY + this.transform.position.y);
// Wait for some time before spawning another object
yield return new WaitForSeconds(spawnInterval);
}
}
}
Comment