- Home /
Question by
FastEddieB · Mar 14, 2013 at 09:09 AM ·
instantiatefor-loopforeach
Instantiate for loop skips some objects
Hi all,
I am having trouble instantiating a series of particle effects from an array of offsets from the transform. The code is as follows:
foreach (Vector3 dir in aoeEffectPositions) {
Vector3 pos = transform.position + dir;
if (game.isOnBoard(pos)) {
GameObject o = Instantiate(firePrefab, pos, Quaternion.identity) as GameObject;
o.GetComponent<ParticleTimer>().Init(game.turnDuration);
}
}
It spawns some of the required effects but randomly omits others. I have tried
A regular for loop
Removing the game.isOnBoard(pos) check
Moving the instantiation and the following line to a coroutine
No dice :/ Any ideas? Cheers in advance
Comment
Answer by FastEddieB · Mar 15, 2013 at 01:25 AM
facepalm
If anyone has a similar issue the problem was with my particle "self destructor" script:
void FixedUpdate () {
if (timeRemaining <= 0.0f)
particleEmitter.emit = false;
if (particleEmitter.particleCount == 0)
Destroy(gameObject);
timeRemaining -= Time.deltaTime;
}
The script was destroying the particle prefab if it did not emit at least one particle immediately. Fixed thusly:
void FixedUpdate () {
if (timeRemaining <= 0.0f) {
timeRemaining = 0.0f;
particleEmitter.emit = false;
if (particleEmitter.particleCount == 0)
Destroy(gameObject);
}
else
timeRemaining -= Time.deltaTime;
}