- Home /
This post has been wikified, any user with enough reputation can edit it.
Calling script method for collider in collision callback
I want to access the opponent game object on collision callback, I am able access the root game object through collider.trasnform.root but how can call a script method on it ? I am getting this error on script compilation " Cannot convert type `UnityEngine.Transform' to 'Opponent' " where 'Opponent' class defined under "Opponent.cs".
Method :
void OnCollisionEnter(Collision collision)
{
Opponent opponent = (Opponent)collision.collider.transform.root.gameObject;
opponent.reduceHealth();
}
Comment
I got the answer here : http://answers.unity3d.com/questions/32352/collision-object-and-access-to-a-script-variable.html thanks.