# How to add & copy ParticleSystem attached to an existing GameObject using C# script in Unity?

I have a GameObject "FireBall" with a ParticleSystem attached to it. I want to add the same ParticleSystem to other GameObject "Avatar". Initially there is no ParticleSystem attached to "Avatar".

When I do this -

ParticleSystem fireparticlesyst = gameObject.AddComponent<ParticleSystem>();


It adds a ParticleSystem to "Avatar" but I want to set all the properties of this ParticleSystem to the ParticleSystem attached to "FireBall".

If I add all the properties by individually I can't set some of the properties as they are read only.

How to do this ?

A simple workaround to do so is:

1- Make an empty GameObject with ParticleSystem attached.

2- Make prefab of that. Borrow that prefab as public GameObject in your script.

3- Instatiate as GameObject in your script.

4- Make child of FireBall and other GameObjects as well.

public GameObject _particleSystem;

void CreatePS()
{
//Instantiate _particleSystem as new GameObject.
GameObject ps = Instantiate(_particleSystem);

// Set new Particle System GameObject as a child of desired GO.
// Right now parent would be the same GO in which this script is attached
// You can also make it others child by ps.transform.parent = otherGO.transform.parent;
ps.transform.parent = transform;

// After setting this, replace the position of that GameObject as where the parent is located.
ps.transform.localPosition = Vector3.zero;
}

• GO to which I want to attach this particlesystem is a movable GO, after doing these steps the particlesystem is coming in the screen but its not moving with the GO. – Shantanu Singh Dec 30 '15 at 7:57
• Make sure in hierarchy that you have set the correct parent – Hamza Hasan Dec 30 '15 at 8:27
• can you show your code? – Hamza Hasan Dec 30 '15 at 9:06
• pastebin.com/qfjedzZA check "FireBall" function – Shantanu Singh Dec 30 '15 at 9:12
• I think ps.transform.parent = transform; will work instead of ps.transform.parent = transform.parent;. It was a mistake. Check the edited answer – Hamza Hasan Dec 30 '15 at 9:19