Hello everybody.
I am having a strange issues with particles.
I created an object, assigned a particle system to it and made a prefab.
The problem is that when i try to instantiate it, the particles don’t move at all, they get created, change shape/colour properly but they stand still; this if i instantiate through code.
If i drag the prefab from the inspector into the scene, everything works fine.
I have this code that take care of creating the particle system:
public class ParticleHelper : MonoBehaviour
{
// Singleton
public static ParticleHelper Instance;
//
public ParticleSystem HeartEmitter;
public ParticleSystem CoinEmitter;
public ParticleSystem StarBurst;
void Awake()
{
// Register the singleton
if (Instance != null)
{
Debug.LogError("Multiple instances created!");
}
Instance = this;
}
// Create an explosion at the given location
public void HeartParticles(Vector3 position)
{
instantiate(HeartEmitter, position);
}
public void CoinParticles(Vector3 position)
{
instantiate(CoinEmitter, position);
}
public void StarParticles(Vector3 position)
{
instantiate(StarBurst, position);
}
// Instantiate a Particle system from the prefab
private ParticleSystem instantiate(ParticleSystem prefab, Vector3 position)
{
ParticleSystem newParticleSystem = Instantiate(prefab, position, Quaternion.identity) as ParticleSystem;
// Make sure it will be destroyed
Destroy(newParticleSystem.gameObject, newParticleSystem.startLifetime);
return newParticleSystem;
}
}
The particles then gets instantiated in an OnTriggerEnter2D() method like this:
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.name == "kiwi")
{
ParticleHelper.Instance.HeartParticles(transform.position);
ApplicationManager.myScore += points;
playerScript.setPower();
Destroy(this.gameObject); //the gameobject associated to this script (the gems) gets destroyed
}
}
Now…the particle collider is set to box, if i set it to anything else the particles move fine but they ignore the shape of the emitter and act like it’s a sphere.
Again, if i drag from the inspector directly into the scene, everything is just fine.
This makes me think that there is something wrong with how they get instantiated…
I tried new scenes, new objects, and even everything new from scratch, but still same behaviour.
Does anybody know why it happen?
I have read a lot of online tutorials and apparently there is nothing wrong.
Is it a bug with the particle system?
Any help would be appreciated.
Thanks.