- Home /
Animator not playing animation on function call from other script
I have a function that plays an animation. It is correctly setup up because when I call it from Start () function, it does play the animation. But, when I call it from another script, the animation simply does not play it.
var animator : Animator;
function Start () {
SwapColors2And5();
}
function Update () {
}
function SwapColors2And5 () {
Debug.Log("2 and 5 called");
animator.Play("colorSwap2And5");
}
This works. But when I try to call SwapColors2And5 from another script, the animator doesn't play it, even though Debug is logging "2 and 5 called".
Here's my call from the other script:
var enemyScript = enemy.GetComponent(enemyController); //this is where I get the actual script
if (randomNumber % 2 == 0 && randomNumber % 3 == 0) {
enemyScript.SwapColors2And3();
}
The function is being called, Debug.Log works, but the animator.play does not.
did you include the var animator : Animator; code line in your other script
you always need a reference
I did not because the animator belongs to the enemy, which hás the main script attached. I tried adding the reference to my other script but it does not work too.
$$anonymous$$odified script:
var animator : Animator = enemy.GetComponent(Animator);
var enemyScript = enemy.GetComponent(enemyController);
if (randomNumber % 2 == 0 && randomNumber % 5 == 0) {
Debug.Log("Number is going to work. It is: " + randomNumber);
enemyScript.SwapColors2And5(animator);
}
//And in the enemy script
function SwapColors2And5 (animator : Animator){
Debug.Log("2 and 5 called");
animator.Play("colorSwap2And5");
}
Any chance that your animator already played selected state? As far as I know, if you invoke the same animation in animator, it wont reset, just keeps playing on.
Nope, the state is never being switched up, it just stays on the default one forever. However, if I call the function from the Start, and not from the other script, the state switches perfectly. It is so strange!
@$$anonymous$$arcelo , let try this. don't call your SwapColors2And5() from start. just call from other script. than it might plays animation. reason of that is explan by @revolute.
Answer by MarceloTerreiro · Dec 24, 2014 at 04:06 PM
I solved my problem. I had to use a different approach, instead of calling the animator from the other script, I get the value of random Number and call the animator inside the main script. Oddly, seems that you can't control animator from other scripts using Get Component.
//I use this to return random number value to my enemy script
function ReturnTheNumber () {
//Call from enemyController
return randomNumber;
}
//And in the enemy script:
public var enemySpawnScript : enemySpawn;
private var randomNumber : int;
private var animator : Animator;
function Start () {
enemySpawnScript = GameObject.FindGameObjectWithTag("Background").GetComponent(enemySpawn);
animator = this.gameObject.GetComponent(Animator);
randomNumber = enemySpawnScript.ReturnTheNumber();
if (randomNumber % 2 == 0 && randomNumber % 3 == 0) {
SwapColors2And3();
}
Your answer
Follow this Question
Related Questions
Animator Controller created by script is not recognized by Animator 2 Answers
Animator slow transitions and animations playing twice. How to set up transitions correctly? 0 Answers
Animator not playing animations 1 Answer
animator controller setTrigger problem 1 Answer
Set Animator Controller Layer Weight 1 Answer