How do I play an animation from another object C#
Hi guys, how can i go about making one object (a switch) activate an animation on another object, what I'm trying to achieve is a secret switch to a door, everything is fine, if i have the script on the switch it states it cannot find the animation on the switch, but the animation is on the door, not the switch, and if i have the script on the door, i get no errors, but doesn't do anything when activated, i just have no idea how to direct the animation from the switch to the door, it seems the only way that I'm getting a result is having the script on the switch not the door, but in this case it is looking for the Anim on the switch not the door.
after a lot of changing and editing to try and rectify this issue, I'm left with this code.
[code]
public bool Open;
public bool doorIsOpening;
public Animator Animation;
public GameObject SecretDoor;
public AudioClip switchSound;
public AudioClip DoorOpen;
// Use this for initialization
void Start () {
Animation = GetComponent<Animator>();
Open = false;
}
private void Update()
{
if (doorIsOpening == true)
{
Open = true;
}
}
public void openSecretRoom () {
doorIsOpening = true;
}
} [/code]
Answer by oswin_c · Dec 22, 2016 at 03:36 AM
If you want the animator of the door, not the switch, you simply call GetComponent on the secret door instead of the switch.
Animation = SecretDoor.GetComponent();.
Thank you so much man, i knew it was something so simple i was missing! cheers!