Hello.
Excecution:
So I’ve got a coroutine that spawns things at a certain location, the script takes a prefab and a spawn point, blends up some random stuff and spawns the prefab
Symptom:
Multiple scripts with the same coroutine are not using their respective variables
Whenever I toss this script onto more than one prefab, the new script (I think) overrides the old scripts.
I believe the stacks are becoming crossed
Failed Solutions:
changing lots to this.etc…
Meat and Potatos
void Start () {
Invoke ("StartSpawning", initialSpawnTime);
Debug.Log ("Start Cleared");
}
void StartSpawning()
{
StartCoroutine (this.SpawnPrefab());
}
private IEnumerator SpawnPrefab()
{
Debug.Log ("SpawnPrefab() ");
while(true)
{
Debug.Log ("True");
FigureSpawnsOut();
Object hClone = GameObject.Instantiate (this.prefab, spawnPoint.position, transform.rotation);
Destroy (hClone, objectLifeSpan);
yield return new WaitForSeconds(SpawnTime());
}
}
Whole Script:
using UnityEngine;
using System.Collections;
public class BasicSpawnHandler : MonoBehaviour {
public GameObject prefab;
public Transform spawnPoint;
public bool randomSpawnPointX;
public bool randomSpawnPointY;
public bool randomSpawnPointZ;
public int randomSpawnX;
public int randomSpawnY;
public int randomSpawnZ;
public float initialSpawnTime;
public float objectLifeSpan;
public bool randomSpawnTimer;
public float baseSpawnTime;
public float randomSpawnTime;
void Start () {
Invoke ("StartSpawning", initialSpawnTime);
Debug.Log ("Start Cleared");
}
void StartSpawning()
{
StartCoroutine (this.SpawnPrefab());
}
private IEnumerator SpawnPrefab()
{
Debug.Log ("SpawnPrefab() ");
while(true)
{
Debug.Log ("True");
FigureSpawnsOut();
Object hClone = GameObject.Instantiate (this.prefab, spawnPoint.position, transform.rotation);
Destroy (hClone, objectLifeSpan);
yield return new WaitForSeconds(SpawnTime());
}
}
public float SpawnTime()
{
if (randomSpawnTimer) {
return Random.Range(baseSpawnTime, randomSpawnTime);
}else{
return baseSpawnTime;
}
}
private void FigureSpawnsOut()
{
Vector3 spawns = spawnPoint.position;
if (randomSpawnPointX)
UpdateSpawnPoint ((float)RandomNumber(spawns.x, randomSpawnX), spawns.y, spawns.z);
if(randomSpawnPointY)
UpdateSpawnPoint (spawns.x, (float)RandomNumber(spawns.y, randomSpawnY), spawns.z);
if(randomSpawnPointZ)
UpdateSpawnPoint (spawns.x, spawns.y, (float)RandomNumber(spawns.z, randomSpawnY));
}
float RandomNumber (float baseNum, int range)
{
return Random.Range((int)baseNum, range);
}
private void UpdateSpawnPoint(float x, float y, float z)
{
spawnPoint.position = new Vector3 (x,y,z);
}
}