Particle Systems are not playing

,

Hello!
I added a spark effect to the weapon when it blocks (the weapon is a prefab instantiated, like the ParticleSystem with the sparks). The Sparks are not in a loop (swing, block, particles are emitted and they die). The PS is attached to the weapon.

Here is the weird thing:
-While I’m in runtime, if I go to the scene directly to the Particle System and hit the button Play, it plays.
-If I do it from a script it does not play (using the old if(!particlesystem.isplaying) particlesystem.play()), but it gets into the part of the code that plays it (used a Debug.Log to check it).
-If I set it on loop and play on awake (from the inspector option in the PS, not the script) it goes wild nonstop (I mean, not even with Stop()).

The code for playing it, is the following:

    [Header("Weapon Effects")]
    [SerializeField] public Transform particleTransform;
    [SerializeField] public ParticleSystem sparkBlock;
    //fire
    //dark
    //etc

    private void Awake()
    {
        Instantiate(sparkBlock, particleTransform);
        sparkBlock=sparkBlock.GetComponent<ParticleSystem>();
        sparkBlock.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear); //tried with this option, not working.
    }

    public void PlayWeaponFX(int effect)
    {
        
        if(!sparkBlock.isPlaying) //failsafe (but, not working)
        {
            sparkBlock.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear);
        }
        else sparkBlock.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear);
        
        //all.Stop();
        if(effect == 0) if(!sparkBlock.isPlaying) sparkBlock.Play();
        else return; //I'll add more effects in the future
    }
}

PlayWeaponFX is called when a block is successfully executed.

Thanks in advance!

What happens if you remove the if statement when you play

//effect should be stopped, but isPlaying may not have reset yet
if(effect==0) sparkBlock.Play();

Nothing changes :confused:

Here is a video: The particle system is there (I can select it and Play it using the button in the Scene view while in runtime).
I added a print(“hey”) when the PS should play, so I know that it is entering in the part of the code that plays the PS.
But when it is activated (blocking and receiving a hit) the print(“hey”) is displayed in the console, but the PS is not being played!
I’m utterly puzzled :confounded:

paste in the PlayerWeaponFX again with the “Hey!” debug in place.

    public void PlayWeaponFX(int effect)
    {
        print(sparkBlockFX.name); //more debuging
        if(!sparkBlockFX.isPlaying) //failsafe (but, not working)
        {
            sparkBlockFX.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear);
        }
        else sparkBlockFX.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear);
        
        //all.Stop();
        if(effect == 0) if(sparkBlockFX.isStopped) {sparkBlockFX.Play(true);print("hey");}
        //else return; //I'll add more effects in the future
    }```

I think I see the problem now. sparkBlock is a prefab reference… but you’re treating it as an in game object… In the first line of Awake() you’re instantiating the prefab, but you’re not saving the instantiated reference, instead you’re working on the prefab itself for the rest of the game. Try this, as is, and see what happens:

    [Header("Weapon Effects")]
    [SerializeField] public Transform particleTransform;
    [SerializeField] public ParticleSystem sparkBlock;
    //fire
    //dark
    //etc

    ParticleSystem sparkBlockInstance;

    private void Awake()
    {
        sparkBlockInstance = Instantiate(sparkBlock, particleTransform);
    }

    public void PlayWeaponFX(int effect)
    {
            if(effect==0) sparkBlockInstance.Play();
    }
}
1 Like

This is going forward somehow haha, at least, I’m getting a different error. When I instantiate the sparkBlockInstance and play the PlayWeaponFX, I get the following error in the console:

MissingReferenceException: The variable instantiateSparkBlockFX of WeaponEffects doesn’t exist anymore.
You probably need to reassign the instantiateSparkBlockFX variable of the ‘WeaponEffects’ script in the inspector.

I know what this error means, the problem is that I don’t know why this is happening.

PS: The particle system is being instantiated in a weapon that is instantiated (same system from the rpg course). The weapon is the longsword in the video.

After instantiating it on Awake, instantiateSparkBlockFX becomes null

All right, I fixed it in a not very sophisticated way that will bring me trouble eventually. Basically I found the issue, it works, but is not the best solution.

The problem is the following: The weapon is instantiated, and in that weapon, I tried to instantiate the Particle System, and failed miserably. So I made a XF GO in the weapon placeholder in the character GO and play de effect there.

Future problem: Change weapons.

End result:

As always, thanks @Brian_Trotter you helped me to understand the issue. I finally made it work, I just had to add the PS to the instantiated Weapon inside a GO and treat it like a GO and that’s it.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.