1
\$\begingroup\$

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 ?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

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;
}
\$\endgroup\$
8
  • \$\begingroup\$ 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. \$\endgroup\$ Dec 30, 2015 at 7:57
  • \$\begingroup\$ Make sure in hierarchy that you have set the correct parent \$\endgroup\$ Dec 30, 2015 at 8:27
  • \$\begingroup\$ can you show your code? \$\endgroup\$ Dec 30, 2015 at 9:06
  • \$\begingroup\$ pastebin.com/qfjedzZA check "FireBall" function \$\endgroup\$ Dec 30, 2015 at 9:12
  • \$\begingroup\$ I think ps.transform.parent = transform; will work instead of ps.transform.parent = transform.parent;. It was a mistake. Check the edited answer \$\endgroup\$ Dec 30, 2015 at 9:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .