- Home /
Question by
ShinobyWuggle · Sep 14, 2016 at 12:30 PM ·
scripting problemspawning problems
Help with object spawning bug
Hi there,
I'm having trouble with a little bug involved in a part of my game where I spawn several objects simultaneously. For some reason, the script will perform as intended for a few seconds, and then the spawn points begin to rapidly spawn objects like crazy (many more than the limit of 3 I include in my script.) I'm not sure where the problem lies on this, and any help is appreciated.
void Update () {
tileSpawner();
}
void tileSpawner()
{
//This section below will run every time that the time clock in game is equivalent to the value
//"cutOff" which is just the game time plus two seconds. It gets reset at the bottom of the statement
//every time it is run, essentially throwing another two seconds on the clock before it gets run again.
//It appears that for whatever reason, the tileSpawner funtion is just being run indefinitely at a certain point.
if (Time.time >= cutOff)
{
x = Random.Range(0, 8);
y = Random.Range(0, 5);
first = x;
tilePrefabs[y] = Instantiate(tilePrefabs[y], spawnLocations[x].transform.position, Quaternion.Euler(0,0,0)) as GameObject;
while(first == x)
{
x = Random.Range(0, 8);
}
second = x;
y = Random.Range(0, 5);
tilePrefabs[y] = Instantiate(tilePrefabs[y], spawnLocations[x].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
while(first == x || second == x)
{
x = Random.Range(0, 8);
}
y = Random.Range(0, 5);
tilePrefabs[y] = Instantiate(tilePrefabs[y], spawnLocations[x].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
cutOff += gapTime;
}
}
}
This is the code that is responsible for destroying the object.
void Start () {
tileDeathPoint = GameObject.Find("Killer");
}
// Update is called once per frame
void Update () {
if(transform.position.y < tileDeathPoint.transform.position.y)
{
Destroy(gameObject);
}
}
}
Comment