- Home /
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) :
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.
Answer by Uhop1903 · Nov 27, 2018 at 01:05 AM
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();
So here is the code that works thanks to both nikigmi end @Deeeds suggestions :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class psStarter : $$anonymous$$onoBehaviour
{
private ParticleSystem bld;
public GameObject ng;
void Start()
{
ng = (GameObject)Instantiate(Resources.Load("ng"), new Vector3(0f, 0f, 0f), Quaternion.identity);
bld = ng.GetComponent<ParticleSystem>();
var main = bld.emission;
bld.Play();
}
Thank you both! Great community here
I also enclose a link explaining why cast is necessary in this particular case, as I didn't know it myself, so it could be useful for someone else :
https://answers.unity.com/questions/1097955/what-is-casting-in-unity.html
Answer by nikigmi · Nov 26, 2018 at 06:35 PM
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();
Answer by Deeeds · Nov 27, 2018 at 01:49 AM
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 ins$$anonymous$$d 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!!!
Your answer
Follow this Question
Related Questions
Creating a GameObject variable without instantiating it? 1 Answer
Instantiating a prefab and values 1 Answer
The object is not output. 0 Answers
Multiple Cars not working 1 Answer