Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MarceloTerreiro · Dec 24, 2014 at 01:19 AM · animatoranimator controller

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.

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ger122 · Dec 24, 2014 at 01:33 AM 0
Share

did you include the var animator : Animator; code line in your other script

you always need a reference

avatar image MarceloTerreiro · Dec 24, 2014 at 02:20 AM 0
Share

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");
 
 }







avatar image revolute · Dec 24, 2014 at 02:28 AM 1
Share

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.

avatar image MarceloTerreiro · Dec 24, 2014 at 02:40 AM 0
Share

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!

avatar image yashpal · Dec 24, 2014 at 06:14 AM 0
Share

@$$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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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();
         } 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

31 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges