Search Unity

Question I am getting errors on line 62 of my code

Discussion in 'Getting Started' started by monkee21, Feb 13, 2024.

  1. monkee21

    monkee21

    Joined:
    Feb 13, 2024
    Posts:
    2
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerMovement : MonoBehaviour
    {
    private Rigidbody2D rb;
    private SpriteRenderer sprite;
    private Animator anim;

    private float dirX = 0f;
    [SerializeField] private float moveSpeed = 7f;
    [SerializeField] private float jumpForce = 14f;

    private enum MovementState { idle, running, jumping, falling }

    // Start is called before the first frame update
    private void Start()
    {
    rb = GetComponent<Rigidbody2D>();
    sprite = GetComponent<SpriteRenderer>();
    anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    private void Update()
    {
    dirX = Input.GetAxisRaw("Horizontal");
    rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);

    if (Input.GetButtonDown("Jump"))
    {
    rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    }

    UpdateAnimationState();
    }

    private void UpdateAnimationState()
    {
    MovementState state;

    if (dirX > 0f)
    {
    state = MovementState.running;
    sprite.flipX = false;
    }
    else if (dirX < 0f)
    {
    state = MovementState.running;
    sprite.flipX = true;
    }
    else
    {
    state = MovementState.idle;
    }

    if (rb.velocity.y > .1f)
    {
    state = MovementState.jumping;
    }
    else if (rb.velocity.y , -.1f)
    {
    state = MovementState.falling;
    }

    anim.SetInteger("state", (int)state);
    }
    }
     

    Attached Files:

  2. monkee21

    monkee21

    Joined:
    Feb 13, 2024
    Posts:
    2
    Line 62 is, else if (rb.velocity.y, -.1f)
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,740
    You can fix your own typing mistakes. Here's how:

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly? Are you structuring the syntax correctly? Look for examples!

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    You can edit your posts. Please post code using code-tags and not plain text.

    You posted the line but you seem to think it's valid C# which it's not. The line preceeding it is "if (rb.velocity.y > .1f)" so this line should likely be "<" rather than a comma and just looks like you're copying code from somewhere and have mistyped. If that's the case then all you really need to do is just look at the code you're copying and check yours. :)

    Thanks.