A
A
Andrey Demidov2021-01-28 20:02:41
Unity
Andrey Demidov, 2021-01-28 20:02:41

The script gives 6 errors most often SetBool,using,Unable to implicitly convert type "UnityEngine.Animator" to "UnityEngine.Animation" How to solve?

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

public class PlayerControler : MonoBehaviour
{
    public float speed;
    public float jumpForce;
    private float moveInput;

    private Rigidbody2D rb;

    private bool facingRight = true;

    private bool isGrounded;
    public Transform FeetPos;
    public float checkRadius;
    public LayerMask whatIsGround;
    

    private Animation anim;


    private void Start()
    {
        anim = GetComponent<Animator>();
        rb = GetComponent<Rigidbody2D>();
    }

    private void FixedUpdate()
    {
        moveInput = Input.GetAxis("Horizontal");
        rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
        if(facingRight == false && moveInput > 0)
        {
            Flip();
        }
        else if(facingRight == true && moveInput < 0)
        {
            Flip();
        }
        if(moveInput == 0)
        {
            anim.SetBool("isRunning", false);
        }
        else
        {
            anim.SetBool("isRunning", true);
        }
    }
    private void Update()
    {
        isGrounded = Physics2D.OverlapCircle(FeetPos.position, checkRadius, whatIsGround);

        if(isGrounded == true && Input.GetKeyDown(KeyCode.Space))
        {
            rb.velocity = Vector2.up * jumpForce;
            anim.SetTrigger("takeOf");
        }

        if(isGrounded == true)
        {
            anim.SetBool("isJumping", false);
        }
        else
        {
            anim.SetBool("isJumping", true);
        }
    }

    void Flip()
    {
        facingRight = !facingRight;
        Vector3 Scaler = transform.localScale;
        Scaler.x *= -1;
        transform.localScale = Scaler;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-01-28
@vabka

you do at the very beginning

using UnityEngine;

public class PlayerControler : MonoBehaviour
{
   //...
    private Animation anim;
    private void Start()
    {
        anim = GetComponent<Animator>();
        //...
    }
   //...
}

You need to change the field type from anim to Animator.
Then all other errors will disappear.
PS1: read the rules, format your questions properly
PS2: learn C# first so you don't trip over such primitive mistakes
PS3: don't shorten variable names. How much time did you save by not adding "ator" to the variable name? I think then you would intuitively understand that something is wrong with this field (why is the animator field of type Animation?)
PS4: Learn to read. In the description of all errors, it is explained in clear text what is wrong in the code. The compiler is 99% smarter than a human and just doesn't write errors.
PS5: If you can't understand the meaning of what is written, google the error code. On the MS site you can find code examples, when they occur and how to fix them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question