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.
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
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:
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.
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.
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.