PARTICLE SYSTEM NEITHER EMITTING NOR PLAYING (BY SCRIPT)

I have a script called “psStarter” attached to the main camera.
This script instantiates a gameobject called “ng” from the Resources folder.
“ng” has a particle system attached as a component.
I want the PS to emit via script once “ng” is instantiated in the scene.
So far I came up with the script below :

public class psStarter : MonoBehaviour
{


    private ParticleSystem bld;
    public GameObject ng;


    void Start()
    {

        Instantiate(Resources.Load("ng"), new Vector3(0f, 0f, 0f), Quaternion.identity);
        bld = ng.GetComponent(typeof(ParticleSystem)) as ParticleSystem;
        Debug.Log("Système de particules récupéré: " + bld);

        var main = bld.emission;
        Debug.Log("Etat du module principal: " + main.enabled);
        Debug.Log("Etat de l'émission du SP: " + bld.isEmitting);

        bld.Play();
        Debug.Log("Etat du système de particules: " + bld.isPlaying);



    }
    void Update()
    {

    }

}

This script gives the following result according to the Debug.Logs inserted in it :

  • The script gets the PS ok.
  • The main module is enabled and “ng” instantiated but the problem is the PS does not emit and does not play.

I have been trying to peck different solutions both from the internet and from the API manual but no result.
I must be missing something, or is Unity reference manuel maybe out of date? I have Unity 2018.2.17f1.

Screenshots :

Hierarchy (showing “ng” instantiated) :

128323-capture-decran-2018-11-25-a-163622.png

Inspector : showing “ng” and its components, including PS :

can you show me what's in the Emission rollout of the particles while it's running?

–

btw, don't feel weird about this. I often have trouble getting particle systems to play. And animations to run. There's so many byzantine and bureaucratic steps that need to be taken to get it to work that it will make you wonder if Unity actually uses their own systems. They don't.

–

also, maybe, for fun, test the time between the start of your particle system and when the callback is called. It'll be interesting to see if it's at one second, two seconds or three seconds after the start.

–

3 Answers

3

use:
ng = Instantiate(Resources.Load(“ng”), new Vector3(0f, 0f, 0f), Quaternion.identity);
instead of
Instantiate(Resources.Load(“ng”), new Vector3(0f, 0f, 0f), Quaternion.identity);

Does it not throw a NullReferenceException in the log at this line:
bld = ng.GetComponent(typeof(ParticleSystem)) as ParticleSystem;
or at this line
bld.Play();

Are these from the same crash report? Its possible that two different client instances (or multiple runs of the same client instance) produced exceptions at different points. Its also possible there is more than one instance of the same class in the scene that could send different exceptions from different points.

–

Thanks for your first answers guys but no progress has been made.
Deeeds you can find enclosed a new screenshot showing the emission rollout, if it helps.
As far as I understood you I tried to add a callbacktest script attached to “ng” but it seems the particle system does not stop as there is no Debug.Log “PS has stopped” displayed :

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

public class CallbackTest : MonoBehaviour {

    public void OnParticleSystemStopped()
    {
        Debug.Log("PS has stopped");
    }
}

nikigmi I tried your suggestion of using ng = Instantiate(Resources.Load(“ng”), new Vector3(0f, 0f, 0f), Quaternion.identity); as a result I get the following error notification “Cannot implicitly convert type’UnityEngine.Object’ to ‘UnityEngine.GameObject’. An explicit conversion exists (are you missing a cast?)”.

Then just cast it. Have you tried that? I think your problem is that your bld is not set to the ParticleSystem you Instantiate You are referencing something else. This is because ng is not set to the object you are instantiating. And @Deeeds is right. Try this. ng = (GameObject)Instantiate(Resources.Load("ng"), new Vector3(0f, 0f, 0f), Quaternion.identity); bld = ng.GetComponent<ParticleSystem>();

–

There are no other instances of the class. I search for it but only one game object has the component. These two exceptions were reported at the same time (literally, but some cases have 1 sec difference). Also same user, same device.

–

Try changing this:

 bld = ng.GetComponent(typeof(ParticleSystem)) as ParticleSystem;

to this:

bld = ng.GetComponent<ParticleSystem>();

the fact that the callback is never getting called seems to indicate the particle system is never starting, hence never ending, so not getting a chance to call that callback.

ps I'm pretty sure, by the extent of your settings, that this must be an awesome particle effect!

–

If the above doesn't work, try placing all your setup inside the Awake() function instead of in the Start() function. Better yet, in your original prefab, make the ParticleSystem a public property and drag the Particle System into it. This will cause a link that can't be broken. If you need info on how to do this, let me know and I'll map out the steps. I've had a lot of "fun" with Unity setups, so know your pain!!!

–