Search Unity

animation switching

Discussion in 'Scripting' started by Hard target, Aug 30, 2014.

  1. Hard target

    Hard target

    Joined:
    Oct 31, 2013
    Posts:
    132
    I am trying to play another animation by accessing another animatorcontroller.How would i correct this script?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ActivateAnimation : MonoBehaviour
    5. {
    6.     void OnCollisionEnter (Collision col)
    7.     {
    8.         if(col.gameObject.name == "prop_powerCube")
    9.         {
    10.             anim = GetComponent<Animator>();
    11.          
    12.          
    13.             anim.runtimeAnimatorController = AnimationforPioyu ;
    14.          
    15.         }
    16.     }
    17. }
     
  2. bbQsauce

    bbQsauce

    Joined:
    Jun 29, 2014
    Posts:
    53
    I don't know much about animation, but from your script i can see that the variable anim is not declared but is used.
    What is AnimationforPioyu ?
    Also is good practice to not use GetComponent after initialization.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class ActivateAnimation : MonoBehaviour
    4. {
    5.     Animator anim;
    6.  
    7.      void Start()
    8.         {
    9.              anim=GetComponent<Animator>();
    10.         }
    11.  
    12.     void OnCollisionEnter (Collision col)
    13.     {
    14.         if(col.gameObject.name == "prop_powerCube")
    15.         {
    16.      
    17.      
    18.             anim.runtimeAnimatorController = AnimationforPioyu ;
    19.      
    20.         }
    21.     }
    22. }
     
  3. Hard target

    Hard target

    Joined:
    Oct 31, 2013
    Posts:
    132
    AnimationforPioyu is a animatorcontroller.