Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

odd animation bug c# 2D

Discussion in 'Scripting' started by EarthenSky, Nov 25, 2014.

  1. EarthenSky

    EarthenSky

    Joined:
    Mar 11, 2014
    Posts:
    41
    hey guys I have a problem in a 2D game i have where the guys swings his axe and it should break a tree and it comes with this error.

    MissingComponentException: There is no 'Animation' attached to the "Tree 1(Clone)" game object, but a script is trying to access it.
    You probably need to add a Animation to the game object "Tree 1(Clone)". Or your script needs to check if the component is attached before using it.
    UnityEngine.Animation.Play (System.String animation)
    Hotbar.Update () (at Assets/Scripts/Hotbar.cs:66)

    And I don't get why the error is there, when I try adding a animation to the tree piece every thing works but the tree spins in a circle whenever I swing my axe.

    Script One Called Hotbar
    Code (CSharp):
    1. public class Hotbar : MonoBehaviour
    2. {
    3.     private treemouse TM;
    4.     public bool TreeBroke = false;
    5.     public GameObject hach;
    6.     public AnimationClip axe;
    7.     public int hotbar = 0;
    8.     void Start ()
    9.     {
    10.         TM = gameObject.AddComponent<treemouse>();
    11.         TM = GetComponent<treemouse>();
    12.     }
    13.     void TreeBreak ()
    14.     {
    15.         TreeBroke = true;
    16.     }
    17.     void Update ()
    18.     {
    19.         if(Input.GetKey(KeyCode.Alpha1))
    20.         {
    21.             hotbar = 0;
    22.         }
    23.         if(Input.GetKey(KeyCode.Alpha2))
    24.         {
    25.             hotbar = 1;
    26.         }
    27.         if(Input.GetKey(KeyCode.Alpha3))
    28.         {
    29.             hotbar = 2;
    30.         }
    31.         if(Input.GetKey(KeyCode.Alpha4))
    32.         {
    33.             hotbar = 3;
    34.         }
    35.         if(Input.GetKey(KeyCode.Alpha5))
    36.         {
    37.             hotbar = 4;
    38.         }
    39.         if(Input.GetKey(KeyCode.Alpha6))
    40.         {
    41.             hotbar = 5;
    42.         }
    43.         if(Input.GetKey(KeyCode.Alpha7))
    44.         {
    45.             hotbar = 6;
    46.         }
    47.         if(Input.GetKey(KeyCode.Alpha8))
    48.         {
    49.             hotbar = 7;
    50.         }
    51.         if(Input.GetKey(KeyCode.Alpha9))
    52.         {
    53.             hotbar = 8;
    54.         }
    55.         if(Input.GetKey(KeyCode.Mouse0))
    56.         {
    57.             if(hotbar == 0)
    58.             {
    59.                 if(TM.TreeMouse == true)
    60.                 {
    61.                     Invoke("TreeBreak", 1);
    62.                 }
    63.                 animation.Play("axe");
    64.             }
    65.  
    66.         }
    67.  
    68.     }
    69. }
    Script Two Called treemouse
    Code (CSharp):
    1. public class treemouse : MonoBehaviour
    2. {
    3.     public bool Broke = false;
    4.     public Transform Main;
    5.     public GameObject woodI;
    6.     private Hotbar HB;
    7.     public bool TreeMouse = false;
    8.     void Start ()
    9.     {
    10.         HB = gameObject.AddComponent<Hotbar>();
    11.         HB = GetComponent<Hotbar>();
    12.     }
    13.     void OnMouseDown ()
    14.     {
    15.         TreeMouse = true;
    16.     }
    17.     void Update ()
    18.     {
    19.         if(HB.TreeBroke == true)
    20.         {
    21.             Instantiate(woodI, Main.position, Main.rotation);
    22.             Instantiate(woodI, Main.position, Main.rotation);
    23.             Instantiate(woodI, Main.position, Main.rotation);
    24.             Destroy(gameObject);
    25.         }
    26.     }
    27. }
    I hope you can help.