Click here to Skip to main content
15,303,151 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Where is the problem?

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using UnityEngine;

public class Player : MonoBehaviour
{
    [SerializeField]
    private float speed = 3.0F;
    [SerializeField]
    private int lives = 5;
    [SerializeField]
    private float jumpForce = 10.0F;

    private bool isGrounded = false;


    private CharState State
    {
        get { return (CharState)animator.GetInteger("State"); }
        set { animator.SetInteger("State", value); }
    }


    new private Rigidbody2D rigidbody;
    private Animator animator;
    private SpriteRenderer Sprite;

    private void Awake()
    {
        rigidbody = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
        Sprite = GetComponentInChildren<SpriteRenderer>();
    }

    private void FixedUpdate()
    {
        CheckGround();
    }

    private void Update()
    {
        State = CharState.Idle;

        if (Input.GetButton("Horizontal")) Run();
        if (isGrounded && Input.GetButtonDown("Jump")) Jump();
    }

    private void Run()
    {
        Vector3 direction = transform.right * Input.GetAxis("Horizontal");

        transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, speed * Time.deltaTime);

        Sprite.flipX = direction.x < 0.0F;

        State = CharState.Run;

    }

    private void Jump ()
    {
        State = CharState.Jump;
        rigidbody.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
    }

    private void CheckGround ()
    {
        Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, 0.1F);

        isGrounded = colliders.Length > 1;
    }


}


public enum CharState
{
    Idle,
    Run,
    Jump
}


What I have tried:

Not to much, for now i'm bad in this
Posted
Updated 18-Dec-20 19:59pm

1 solution

Look at error, and it gives you three pieces of information:
Assets\scriprts\player.cs(21, 44): error CS1503: argument 2: cannot convert from 'charstate' to 'int'

Assets\scriprts\player.cs
The file the error was found in
(21, 44):
The line (21) and column (44) that it found the problem.
error CS1503: argument 2: cannot convert from 'charstate' to 'int'
The problem it found.
So open the file, go to line 21, and look at it (most text editors will use CTRL+G to go to a specific line number, or you can double click the error message in VS and it will go there):
set { animator.SetInteger("State", value); }
                                   ^
                                   |
                                    ----- Column 44

Now read the error message: "cannot convert from 'charstate' to 'int'"
And it's pretty obvious: you cannot convert a charstate value - which is what you are passing to the property setter - to an integer - which is what the method expects.

Now, I have no idea what a "charstate" is, so I can't be specific - but you either need to cost it, convert it, or provide an explicit and / or implicit casting operator to your class in order to do that!
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900