I used the following code to add rigidbody and collider to a gameobject in runtime.
var l_palm = GameObject.Find("Player/man/Pelvis/Spine/Spine.2/Heart/L_Shoulder/L_Elbow/L_Hand/L_Palm");
var collider_l_palm = l_palm.AddComponent(SphereCollider);
var rigidbody_l_palm = l_palm.AddComponent(Rigidbody);
Now, I need to add Particles, audio, material to the same gameobject when collide.
function OnCollisionEnter(hit : Collision)
{
if(hit.gameObject.tag == "lightpunch")
{
Character.animation.Play ("beingattack");
//how to add particles, audio, material here?
}
}
here is the components that i need to add and how should I access them to set their properties?
The easiest way to do this is to set up the particle effect and sound on an empty GameObject in the scene and then make a prefab from it. You can then instantiate the prefab wherever you want to release the particles. In your code, you would probably use something like this:-
var effectPrefab: GameObject;
function OnCollisionEnter(hit : Collision)
{
if(hit.gameObject.tag == "lightpunch")
{
Character.animation.Play ("beingattack");
Instantiate(effectPrefab, hit.contacts[0].position, Quaternion.identity);
}
}
If you enable the One Shot and Autodestruct options on the particle system, the effect object will be destroyed when the last particle has disappeared.