1

(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 ?

Lee Paulison
  • 141
  • 1
  • 8
  • 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 Mar 07 '19 at 17:06
  • 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 https://docs.unity3d.com/ScriptReference/Component.GetComponentInChildren.html or atleast add a check to see if you have a child at index 0 before calling its gameobject. – AresCaelum Mar 07 '19 at 17:12
  • 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 Mar 07 '19 at 17:14
  • @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 Mar 07 '19 at 17:16
  • @Eddge what I think is he is following a wrong approach to instantiate/access the particle system. Regardless how he is trying to start it. And as I see the first link is what he used to write his code, the other links just didnt provide him an answer. – Ignacio Alorre Mar 07 '19 at 17:19
  • @IgnacioAlorre his code has no mention of instantiating a particle system, he has a method he uses to get a particle system from a child gameObject(Which differs from the link). Which would lead me to think the ParticleSystem already exists at this point. If the OP comes back to answer my first question, and says that line is never being called then I will look into whether or not he is properly instantiating. There is no point in troubleshooting something if we have no idea if it is an issue yet. – AresCaelum Mar 07 '19 at 17:22
  • 1
    @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 Mar 07 '19 at 17:50
  • @LeePaulison well done, but instead of changing to solved in the title, add in an answer your findings and mark your own answer as accepted. So other people can use those lines to fix similar issues. – Ignacio Alorre Mar 07 '19 at 17:59

1 Answers1

2

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.

Lee Paulison
  • 141
  • 1
  • 8