Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Start a specific particles with a script

Discussion in 'Scripting' started by Faltren, Feb 1, 2017.

  1. Faltren

    Faltren

    Joined:
    Jan 10, 2017
    Posts:
    17
    Hello,
    I a little issue about particles.
    I have a Bullet in a weapon and when I click, my weapon shoot my bullet and some particles are played (there are played automatically when the bullet is instantiate). But when the bullet hit something, I destroy it but I also want to start particles. The problem is that I have 2 particles in my bullet and I can't Get any of them (and so start them).

    So my question is, how can I start my particles in a script during the "destroying-of-my-bullet". ?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    Assuming:

    1. the particles are part of the bullet
    2. the bullet is destroyed on impact

    Then the particles will be destroyed at the same time the bullet is.

    To fix this:

    1. make the particles a separate prefab, OR
    2. detach the particles from the bullet by making it a child of the bullet, and then setting its parent to null.
     
  3. Kamil-Says

    Kamil-Says

    Joined:
    Jun 30, 2014
    Posts:
    154
    Simple solution:

    Instantiate particle prefab and set the transfrom position to the bullet hit point (position), then play & destory it after playtime.

    Code (CSharp):
    1. void OnHit()
    2. {
    3.    Gameobject particle = GameObject.Instantiate(ParticleObj,Vector.zero.Quaternion.identity) as GameoObject;
    4.  
    5. particle.transform.position = bullet.transform.position;
    6. particle.GetComponent<ParticleSystem>().Play();
    7.  
    8. }
     
  4. Faltren

    Faltren

    Joined:
    Jan 10, 2017
    Posts:
    17
    Ok I will try to separate prefab :) thx.


    Nice thk, I will try it tonight :)