2d Bullet hit particle

I am new to programming and I am trying to make my tiny bullet hit particle system show when my cloned bullets collide with anything

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class bullet : MonoBehaviour
{
    //particle prefab
    public GameObject impact;

    public float speed = 20f;
    public int damage = 20;
    public Rigidbody2D rb;

    // Start is called before the first frame update
    void Start()
    {
        rb.velocity = transform.up * speed;
    }

    private void OnTriggerEnter2D(Collider2D hitInfo)
    {
        enemyDamage enemy = hitInfo.GetComponent<enemyDamage>();

        //trying to clone the effect for each bullet
        impact = Instantiate(impact, transform.position, Quaternion.identity) as GameObject;
        if (enemy != null)
        {
            enemy.takeDamage(damage);
        }
        Destroy(gameObject);
        Destroy(impact, .75f);
    }

    // Update is called once per frame
    void Update()
    {
       
    }
}

Fixed this by instantiating my particle effect in a new method

void impactPlay()
    {
        impact = Instantiate(impact, bulletEnd.transform.position, Quaternion.identity);
        impact.GetComponent<ParticleSystem>().Play();
        Destroy(impact, .8f);
    }

You don’t need to do that, just check “Play on Awake” and set “Stop Action” to destroy in the particle system’s settings

1 Like