I want to do something so simple, it annoys me that I can't get it to work.
I have a 3d object, and it should play a diffrent animation depending on wich place on the object you click, so depending on wich collider your mouse is over.
Now I know you can create children and add more thene one collider this way, but if I call an animation in the child's script it can't find it since it's not the same object, it's a child.
So basiclly, how do I call an animation of the main object? not of the child.
I am not sure I understand completely, but try using Transform.parent to work your way up the chain. Once you have the root transform it will be trivial to get the animation component and play your animation through a GetComponent. For instance Transform.parent.gameObject.GetComponent()
var clip : String = "DefaultClip"; // Change in inspector to what you want.
function OnMouseDown() {
transform.parent.animation.Play(clip);
}
using UnityEngine;
public class PlayClipOnClick : MonoBehaviour
{
public string clip = "DefaultClip"; // Change in inspector to what you want.
void OnMouseDown()
{
transform.parent.animation.Play(clip);
}
}
You could also make this a bit "smart" to see if designer has set an animation, if not, see if there's an animation on this object, if not, see if there's an animation on parent object.
using UnityEngine;
public class PlayClipOnClick : MonoBehaviour
{
public string clip = "DefaultClip"; // Change in inspector to what you want.
public Animation anim = null; // Possible to set in inspector.
void Start()
{
anim = anim ?? animation;
if (transform.parent)
anim = anim ?? transform.parent.animation;
}
void OnMouseDown()
{
anim.Play(clip);
}
}