- Home /
Making an Animation pause function in a different game object
I am working on animating a door in my game. I have a parent game object that hosts the door object. This is because the box collider rotated with the door when it was attached to it, so now, since the collider is on the parent, it will not rotate. But since the script is on the parent calling the animator component from the child, it cannot be used for an animation event. I originally had
private void pauseAnimationEvent(){
anim.enabled = false;
}
This is so it would stop the door at a specified point so that it stayed open until the character went out of the box collider's area and triggered the door to close. My question is, is there a way to call this function to an animation event that is on the animation of the door which is the child of the object that the script is on? I know it is a weird question, but I would appreciate any help.
Answer by highpockets · Apr 24, 2019 at 09:48 PM
The parent object can have a reference to the child script in a script on the parent like so:
public ChildScriptName childScript; //drag the child script into the field in the inspector, has to be the child instance of the script
void Update(){
//Call the function
childScript.pauseAnimationEvent();
}
That should do it.
Cheers
@highpockets I am slightly confused. Should I make an entirely different script for the child and do the pauseAnimationEvent() { anim.enabled = false; } in the child and then reference that in the void update of my parent script? Is that what you are saying? I just needed to clarify.
The script that has pauseAnimationEvent() in it is the ChildScript I was referring to in my answer. I believe you have the pauseAnimationEvent() in a script on the child. To get reference to the script that contains that method, you create the reference like I did above. Though the pauseAnimationEvent() method needs to be public in order to access it with another script. @$$anonymous$$-ANator also has a viable solution below. As you can also access the animator component of the child and just place the method in there.
@highpockets I did exactly what you told me to do, and now it is giving me error CS0122: `PauseEvent.pauseAnimationEvent()' is inaccessible due to its protection level. Do you have any idea as to what is going on or how I should fix it?
That means that the method is private. Did you make it public like I mentioned??
public class PauseEvent : $$anonymous$$onoBehaviour
{
public void pauseAnimationEvent(){
//do what you do
}
}
And in the script that you want to reference it from:
public PauseEvent pE;
void Update(){
p$$anonymous$$pauseAnimationEvent();
}
Answer by K-Anator · Apr 25, 2019 at 04:03 AM
I'm a little confused as to how you have your scene setup. If I understand correctly, you have a trigger collider, with a door as a child. If this is the case, and you want to control the animator in the child, you should be able to do something along the lines of this:
public class DoorAnim : MonoBehaviour
{
private Animator anim;
private void Start()
{
anim = GetComponentInChildren<Animator>();
}
private void pauseAnimationEvent()
{
anim.enabled = false;
}
That should, in theory allow you to control an animator which is on a child of the object holding your script.
The thing is, the trigger collider and the script are on the parent, and the animation is on the child. I want to do the pause animation event function in the script without linking the script to the child. I already have the get component in children programmed, I just need to know how to tie a pause animation event to an animation event that is not directly tied to the script. Is this possible? P.S. I also had that exact code in my script and Unity gave me an error saying that the animation event did not have a direct call, so it couldn't work.