- Home /
C# Acessing animator on another Object
Hey there, I am currently trying to trigger an animator bool with the new 2D Animator tool, what I need to do is this:
animator.SetBool("Level2", false );
But from another script on another object, and so far it does not work the usual way, by doing:
public GameObject myObject;
myObject.animator.SetBool("Level2", false );
Any help is appreciated! :D
$$anonymous$$ake sure your myObject
is holding a valid reference. Access its Animator
via GetComponent. $$anonymous$$ake also sure that the animator controller referenced there has the Level2 parameter defined (and that it's a bool!).
@frarees I don't seem to be able to use "GetComponent" can it be that animator is private so that it is not accesible? :)
Don't understand what you mean by private animator.
public GameObject otherObject;
Animator otherAnimator;
void Awake () {
otherAnimator = otherObject.GetComponent<Animator> ();
}
Does this work for you? What errors/exceptions is Unity throwing?
@frarees Sorry about the private animator thing, my fault. And yes it works like a charm now! Thank you so much for taking your time out to help me :D
Answer by frarees · Mar 17, 2014 at 03:47 PM
public GameObject otherObject;
Animator otherAnimator;
void Awake () {
otherAnimator = otherObject.GetComponent<Animator> ();
}
If otherObject references to a GameObject that has an Animator component on it, otherAnimator will contain a valid reference.
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Having multiple 2D Characters 1 Answer
Start sprite animation at the same index that the previous animation stopped 2 Answers
Multiple Cars not working 1 Answer