Problem scripting with animations ( im new ).

Hey! I am new to unity i tried to do some things for my self like playing the fly animation when my player jumps for some reason it doesn’t work here is my script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class character : MonoBehaviour
{
    public float speed = 6.0f;
    public float rotateSpeed = 6.0f;
    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;
    private Animation anim;

    private Vector3 moveDirection = Vector3.zero;
    private CharacterController controller;
    private int jumps;
    public AnimationClip fly;
    public AnimationClip idle;
    // Start is called before the first frame update
    void Start()
    {
        controller = GetComponent<CharacterController>();
        anim = gameObject.GetComponent<Animation>();
      
        anim.AddClip(fly, "fly");
        anim.AddClip(idle, "idle");
    }

    // Update is called once per frame
    void Update()
    {
        if (controller.isGrounded)
        {


            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection *= speed;
            if(Input.GetButtonDown("Jump"))
            {
                moveDirection.y = jumpSpeed;
               
            }
        }

        moveDirection.y -= gravity * Time.deltaTime;
        controller.Move(moveDirection * Time.deltaTime);


        if (Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log("Jumping");
            anim.Play("fly");
        }
      
       
          
       
    }
}

the console says this:
MissingComponentException: There is no ‘Animation’ attached to the “devil@pose” game object, but a script is trying to access it.

You probably need to add a Animation to the game object “devil@pose”. Or your script needs to check if the component is attached before using it.

UnityEngine.Animation.AddClip (UnityEngine.AnimationClip clip, System.String newName, System.Int32 firstFrame, System.Int32 lastFrame) (at C:/buildslave/unity/build/artifacts/WinEditor/modules/Animation/AnimationsBindings.gen.cs:400)
UnityEngine.Animation.AddClip (UnityEngine.AnimationClip clip, System.String newName) (at C:/buildslave/unity/build/artifacts/WinEditor/modules/Animation/AnimationsBindings.gen.cs:390)
character.Start () (at Assets/character.cs:24)

Images are added to my thread
Thanks so much for helping and reading this i know its a lot im just trying to understand what i did wrong and how to change this or do this in a better way ( wanna add walk attack running animations later to any tips for that? )

5010104--490031--WOW HELP 3.JPG


private Animator anim;

you will want to change your variable to animator rather than animation. Then when you try to access the animator with getcomponent anim = GetComponent<Animator>()

then as for the animations go, you don’t have to access the animation clips by defining them as variables, you could use triggers or change the states of the animation clips with conditions in the animator window then in code where ever you need the transitions to happen put anim.SetBool("Some variable in the animator window you defined", true); then set it to false when it needs to transition back, then something similar with triggers like it seems you wanna do. Hope this helps in some way.

just a reminder to make sure you have the actual component on the object you are using this script on, so it can actually access it.

Hey thank you so much this really helped it now works when i press jump my bool will turn on and then it will turn off but thats also the problem

I want my character to fly when its in the air and now i think about it i did it wrong because if i press jump one time the bool that i set as Fly will turn on for like 1 second and then i almost can’t even see the animation going on.

Does someone know how to make my character do the flying animation when it is in the air?
Should i just set the exit time?

I know it wouldn’t really fix it if my character was jumping from really high

So i have like an game idea that my friends can play as this character ( we fight eachother we press the left mouse button and a fire ball will shoot )
the gravity is low
we can double jump
and then i when we are in the air the fly animation plays
and when i attack the attack animation plays ( i know how to do this but first i will work on the fly animation )

and also my devil is kind of flying above the ground and not standing on the ground

i have an character controller
and an animator with apply root motion turned on any way to fix this?

Thank you guys so much sry if my english is bad.

Also i have another problem
When my character jumps it can’t really move in the air and for my game well it needs to move in the air is there an simple fix for this ? Or do i really need to change the script… thx guys sry if its obvious like im really a beginner at scripting