Particle system play on event set in animation clip

Hi,

I have a particle system put into the hierarchy of the game object and I want to trigger it using an event set on the animation clip(fbx file with a sword attack). The script has no errors until I activate the animation in the build. I get this error and I have no idea how to solve it:
"
NullReferenceException: Object reference not set to an instance of an object
ParticleEvents.StartAttack01 () (at Assets/ParticleEvents.cs:22)
"
I have a simple script attached to the game object containing the character and the particle system. What more am I suppose to add? I did name the events attached to the animation using the same naming convention as lower.

public class ParticleEvents : MonoBehaviour
{

ParticleSystem SwordSlash_EFF;

void Start()
{

}

void Update()
{

}
public void StartAttack01()
{
SwordSlash_EFF.Play();
}
public void EndAttack01()
{
SwordSlash_EFF.Stop();
}
}

Thank you!

I don’t think you’ve got a handle to the particle system in other words SwordSlash_EFF is by default set to null. If the particle system is on the same object as the script add this to the Start Method: SwordSlash_EFF = GetComponent();

If the particle system isn’t on the same object as the script the easiest solution is to make the Particle system public
e.g. public ParticleSystem SwordSlash_EFF; and then assigning the particle system in the inspector.

1 Like

Oh wow, the public variant worked like a charm, yes the particles weren’t directly attached to the game object.
Thank you so much!!! Awesome!

1 Like