Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help Please! Cannot assign the right animation to a Script / GameObject!

Discussion in 'Scripting' started by harrypotterindy, May 15, 2015.

  1. harrypotterindy

    harrypotterindy

    Joined:
    Dec 27, 2014
    Posts:
    15
    Hi,
    I am not able to assign the animation I want to assign to a Script on a GameObject. I have amde the aim animation, but it still wants me to assign the shoot animation to the aim animation. I have tried dragging and dropping, that didn't work, I have tried changing where I assign the animation (when I press the circle icon and then in the top left corner of the pop-up I press scene or assets), and when I do assign the animation, nothing happens. Here is my code:


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class MachinegunShooting : MonoBehaviour {
    6.  
    7.    public float CoolDownTime = 0.2f;
    8.  
    9.    public Animation shootAnim;
    10.    public Animation aimAnim;
    11.  
    12.    public GameObject Machinegun;
    13.    public GameObject zombie;
    14.    
    15.    //Cooldown
    16.    private float CoolDown;
    17.    
    18.    // Use this for initialization
    19.    void Start()
    20.    {
    21.      Machinegun.SetActive(true);
    22.    }
    23.    
    24.    // Update is called once per frame
    25.    void Update()
    26.    {
    27.      //CoolDown<Time.time means that current game time is greater than our cooldown time which means we can fire again
    28.      if (Input.GetKey(KeyCode.Mouse0) && CoolDown<Time.time)
    29.      {
    30.        Ray ray = new Ray(transform.position, transform.forward);
    31.        RaycastHit hit;
    32.  
    33.        GetComponent<AudioSource>().Play();
    34.        GetComponent<Animation>().Play();
    35.  
    36.        //CoolDown = current in game time + cooldown time
    37.        CoolDown = Time.time + CoolDownTime;
    38.  
    39.        if (Physics.Raycast(ray, out hit))
    40.        {
    41.          if(hit.collider.CompareTag("Zombie")) {
    42.            Destroy(zombie);
    43.          }
    44.        }
    45.      }
    46.  
    47.      if (Input.GetKey (KeyCode.Mouse1))
    48.      {
    49.        aimAnim.Play();
    50.      }
    51.    }
    52. }
    53.  
    Please pay no attention to the zombie stuff it is just temporary to make stuff work. I just added that so I can come back and edit it later and then it will be better. Thank you. If you want to help me make this Zombie FPS game, you can always E-Mail me at:
    harper@harpernicholson.ca
    Please also keep in mind that I am only 10 years old and do not have much experience with coding.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    is there a specific reason for using the legacy approach of directly manipulating the animations rather than using an Animator controller component?
     
  3. harrypotterindy

    harrypotterindy

    Joined:
    Dec 27, 2014
    Posts:
    15
    I'm not too sure of what you mean for me to change.
     
  4. Nitugard

    Nitugard

    Joined:
    May 10, 2015
    Posts:
    341
    Try changing Animation to AnimationClip.
     
  5. harrypotterindy

    harrypotterindy

    Joined:
    Dec 27, 2014
    Posts:
    15
    I have changed to animationclip but I am getting an error:
    Assets/Scripts/MachinegunShooting.cs(50,33): error CS1061: Type `UnityEngine.AnimationClip' does not contain a definition for `Play' and no extension method `Play' of type `UnityEngine.AnimationClip' could be found (are you missing a using directive or an assembly reference?)

    Here is my code:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class MachinegunShooting : MonoBehaviour {
    6.  
    7.    public float CoolDownTime = 0.2f;
    8.  
    9.    public AnimationClip shootAnim;
    10.    public AnimationClip aimAnim;
    11.  
    12.    public GameObject Machinegun;
    13.    public GameObject zombie;
    14.    public GameObject bulletHole;
    15.    
    16.    //Cooldown
    17.    private float CoolDown;
    18.    
    19.    // Use this for initialization
    20.    void Start()
    21.    {
    22.      Machinegun.SetActive(true);
    23.    }
    24.    
    25.    // Update is called once per frame
    26.    void Update()
    27.    {
    28.      //CoolDown<Time.time means that current game time is greater than our cooldown time which means we can fire again
    29.      if (Input.GetKey(KeyCode.Mouse0) && CoolDown<Time.time)
    30.      {
    31.        Ray ray = new Ray(transform.position, transform.forward);
    32.        RaycastHit hit;
    33.  
    34.        Machinegun.GetComponent<Animation>().Play();
    35.        Machinegun.GetComponent<AudioSource>().Play();
    36.  
    37.        //CoolDown = current in game time + cooldown time
    38.        CoolDown = Time.time + CoolDownTime;
    39.  
    40.        if (Physics.Raycast(ray, out hit))
    41.        {
    42.          Instantiate(bulletHole, hit.collider.transform.position, hit.collider.transform.rotation);
    43.          if(hit.collider.CompareTag("Zombie")) {
    44.            Destroy(zombie);
    45.          }
    46.        }
    47.      }
    48.  
    49.      if (Input.GetKey (KeyCode.Mouse1))
    50.      {
    51.        aimAnim.Play();
    52.      }
    53.    }
    54. }
    55.  
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    under the legacy system, AnimationClips are held in an Animation component. "Play" is a function of the Animation component, and can take an AnimationClip as a parameter. If you don't use a parameter the default clip is played.

    http://docs.unity3d.com/ScriptReference/Animation.html
    http://docs.unity3d.com/ScriptReference/Animation.Play.html

    have a look at the unity manual: http://docs.unity3d.com/Manual/AnimationSection.html

    this is all "legacy" system, as in "the old way of doing it". The newer way is via the Mecanim system (again, covered in the manual linked above).