- Home /
Question by
Ahmed_n1 · May 10, 2014 at 04:28 PM ·
gameobjectprefabparticlesystem
How to add a prefab containing Animation to a GameObject?
I have a gameobject in my scene. And I have a prefab with a particle system animation in it. When I drag&drop the prefab on my gameobject it is added as its child and the animation appears on the object. I need to do the same but from script. I searched a lot and here is my code based on what I found so far.
GameObject objCandle = GameObject.Find("candle");
UnityEngine.GameObject obj = (GameObject)Resources.Load("Candle_Fire");
Debug.Log(obj.name + " " + obj.GetType().ToString());
GameObject ps_candle = (GameObject)GameObject.Instantiate(obj,
objCandle.transform.position, objCandle.transform.rotation);
obj.transform.parent = objCandle.transform;
ParticleSystem ps = objCandle.GetComponentInChildren<ParticleSystem>();
ps.Play();
but it is not working. There are no exceptions, I debugged the code. But nothing is working. The animation is not starting. I even tried to make the particle system animation to "Play on Awake" but nothing happened too.
What's wrong?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613144721im_/https://answers.unity.com/themes/thub/images/avi.jpg)