- Home /
Question by
guillopuyol · Feb 07, 2014 at 06:26 AM ·
particle systemgetcomponentinchildren
GetCompontentInChildren erratic behaviour
Hello,
I have a game object with a sphere collider and a child named HitParticles. The child HitParticles has a ParticleSystem component. I want the HitParticles to show when there is a trigger enter, but It works erratically. I hit play to run the game, and it works as expected. I stop the game (in Unity), run it again, and it no longer works...
void OnTriggerEnter(Collider other) {
if (other.tag=="Fruit"){
//Localize fruit object
fruit = other;
//Emit particles show if the object has one
if(fruit.GetComponentInChildren<ParticleSystem>()){
Debug.Log("Emiting Particles"); //This debug doesn't fire when it fails...
_particleFX = fruit.GetComponentInChildren<ParticleSystem>();
waitToDestroyTimer = _particleFX.duration;
if (_particleFX.tag=="ShootingParticles"){
Vector3 _emiterPosition = _particleFX.transform.position;
Vector3 particleDestination = new Vector3(_emiterPosition.x + Random.Range(-300, 300), _emiterPosition.y + Random.Range(-100, 300), _emiterPosition.z + Random.Range(-300, 300));
_particleFX.transform.position = Vector3.Lerp(_emiterPosition,particleDestination ,0.1f);
}
_particleFX.Play();
}
Comment
Have you tried to display other.name
when TriggerEnter to be sure that what triggers your component is composed of a ParticleSystem ?