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

How to add a copy of Particle System of another object in scene using C# script ?

Discussion in 'Scripting' started by shantanusingh994, Dec 29, 2015.

  1. shantanusingh994

    shantanusingh994

    Joined:
    Dec 26, 2015
    Posts:
    16
    I want to add a Particle System to my gameobject and I want to copy the properties of this particle system from a particle system attached to one other object present in the scene.

    That other object named "FireBall" is present as a child of "Avatars" gameobject. Avatar gameobject is disabled from the scene (by unchecking the checkbox at the top).

    I am doing this -
    Code (CSharp):
    1. ParticleSystem fireparticlesyst = gameObject.AddComponent<ParticleSystem>(FireBall.particleSystem);
    It is showing an error. How to do this ?
     
  2. Mehrdad995

    Mehrdad995

    Joined:
    Jul 17, 2013
    Posts:
    46
    Not intending to retrieve an old thread but as this thread apparently pops up first on a Google search I'll answer for future refers.
    There's no simple method as you expected, you have to copy all variables of the reference to the destination but fortunately, you can do it in a generic form and reuse it everywhere.
    Please refer to THIS post.
     
  3. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,904
    99% of the time you'd use a prefab. Don't put the fireball on any object. Make a prefab fireball. The first object Instantiates a copy, and so does the second (the manual on prefabs has decent examples).

    The other thing is if you want a perfect copy of a particle system the exact way it looks in the current frame. That's difficult, and probably no one would notice, and even if you did, after some time randomness would make them different again.
     
    Mehrdad995 likes this.