Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Particle system play on event set in animation clip

Discussion in 'Scripting' started by igotsuss, Apr 24, 2021.

  1. igotsuss

    igotsuss

    Joined:
    Feb 15, 2021
    Posts:
    5
    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!
     
  2. EDDDDDDD

    EDDDDDDD

    Joined:
    Sep 30, 2017
    Posts:
    3
    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<ParticleSystem>();

    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.
     
  3. igotsuss

    igotsuss

    Joined:
    Feb 15, 2021
    Posts:
    5
    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!
     
    EDDDDDDD likes this.