1
votes

(Please excuse any formatting issues) I am trying to get a simple particle system to play with an OnTriggerEnter and stop with OnTriggerExit. Following the Unity API on particle systems (https://docs.unity3d.com/ScriptReference/ParticleSystem.Play.html). I developed the following code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Electric_Trap_Trigger : MonoBehaviour
{

ParticleSystem system
{
    get
    {
        if (_CachedSystem == null)
            _CachedSystem = transform.GetChild(0).gameObject.GetComponent<ParticleSystem>();
        return _CachedSystem;
    }

}

private ParticleSystem _CachedSystem;

public bool includeChildren = true;

//Start is called before the first frame update
void Start()
{
    if (system != null)
        Debug.Log("Trap found");
}

// Update is called once per frame
void Update()
{

}

private void OnTriggerEnter(Collider other)
{
    if(other.gameObject.tag == "Player")
    {
        Debug.Log("Trap Triggered by: " + other.gameObject.tag);
        if(system != null)
        {
            system.Play(includeChildren);
        }
    }
}

private void OnTriggerExit(Collider other)
{
    if (other.gameObject.tag == "Player")
    {
        Debug.Log("Trap Exited by: " + other.gameObject.tag);
        if (system != null)
        {
            system.Stop(includeChildren);
        }
    }
}

}

As you can see I have debugging code that reports the particle system has been found and the player object does indeed interact with the box collider. The particlesystem does not play. Any help would be greatly appreciated.

Answers Reviewed: playing particle system in Unity How to start and stop a particle system in Unity ? Properly play Particule System component? How to start and stop a particle system in Unity ?

1
have you placed a check to make sure you do indeed have a particle system? put a debug statement in if(system != null). - AresCaelum
Also, your code is error prone, if you ever change the "player" tag this code will never run, not to mention if the associated object doesnt have a child at index 0, you will get a null reference error. It may be best to take a look at the function GetComponentInChildren documentation at docs.unity3d.com/ScriptReference/… or atleast add a check to see if you have a child at index 0 before calling its gameobject. - AresCaelum
From your link: The following example creates a GUI window for manipulating a Particle System. Is that what you really need? A GUI to manipulate that Particle System? Or you just need to programatically start and stop a Particle System? - Ignacio Alorre
@IgnacioAlorre regardless of it being a way to create a GUI Window the functionality of the play command is the same, that is making a particle system play. His other links do not lead to only GUI related articles. - AresCaelum
@Eddge The system does indeed exist according the the debug code within my Start method. Also, the ParticleSystem is a child of the GameObject with a BoxCollider set to IsTrigger. They both exist within the scene space as active objects. Lastly - I just solved this issue, which I will mark as solved. Apparently, I needed to set the ParticleSystem to Prewarm. Changing that option to true enabled the system to work as scripted. I don't know why, and that is certainly something for research. Thank you all for your answers. - Lee Paulison

1 Answers

2
votes

As suggested:

The answer to my particular problem: "How to get an existing Unity ParticleSystem to play through script" (slightly more descriptive title). Has been solved. Please note further research is required to understand why the answer worked.

One setting in the ParticleSystem is called Prewarm. Enabling that setting allowed the system.Play and system.Stop code to start and stop the particlesystem. As previously stated, I as yet do not know why changing that setting to true (enabled) allowed the code to work.

No proper research was done. I was simply playing with settings, one at a time.