Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question I GET THIS ERROR AND I DONT KNOW HOW TO FIX IT

Discussion in 'Scripting' started by LynxXd, Jan 21, 2023.

  1. LynxXd

    LynxXd

    Joined:
    Jan 21, 2023
    Posts:
    6
    IM TRYING TO WRITE THE WALKING SCRIPT 4 A CHARACTER N I GET HIS S*** PLS HELP


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7.  
    8. {
    9.     private float horizontal;
    10.     private float speed = 8f;
    11.     private float jumpingPower = 16f;
    12.     private bool isFacingRight = true;
    13.  
    14.     [SerializeField] private Rigidbody2D rb;
    15.     [SerializeField] private Transform groundCheck;
    16.     [SerializeField] private LayerMask groundLayer;
    17.  
    18.      // Update is called once per frame
    19.  
    20.     void Update()
    21.     {
    22.         horizontal = Input.GetAxisRaw("Horizontal");
    23.  
    24.         if (Input.GetButtonDown("Jump") && IsGrounded)
    25.         {
    26.             rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
    27.         }
    28.  
    29.         if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
    30.         {
    31.             rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
    32.         }
    33.  
    34.         Flip();
    35.     }
    36.  
    37.     private void FixedUpdate()
    38.     {
    39.         rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
    40.     }
    41.  
    42.     private bool IsGrounded()
    43.     {
    44.         return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
    45.     }
    46.  
    47.     private void Flip()
    48.     {
    49.         if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
    50.         {
    51.             isFacingRight = !isFacingRight;
    52.             Vector3 localScale = transform.localScale;
    53.             localScale.x *= -1f;
    54.             transform.localScale = localScale;
    55.         }
    56.     }
    57. }
    58.  
    IM TRYING TO WRITE THE WALKING SCRIPT 4 A CHARACTER N I GET HIS S*** PLS HELP
     
  2. rickitz5

    rickitz5

    Joined:
    Aug 8, 2021
    Posts:
    31
    I mean if you want help, you need to actually explain the issue. You cannot just say you get an error, what error are you receiving? Also if the character is not doing exactly what you want, please explain the intended behaviour, and the behaviour that is actually happening. You will unlikely get help on a forum with what you have typed.
     
    LynxXd likes this.
  3. LynxXd

    LynxXd

    Joined:
    Jan 21, 2023
    Posts:
    6
    Mb I'm trying to make my first prototype of a platformer game, I was trying to make the script for the movement by watching this yt video
    n it gives me that error n I don't know how to make it work
     
  4. LynxXd

    LynxXd

    Joined:
    Jan 21, 2023
    Posts:
    6
    the code was the exactly same n the steps but it still doesn't work
     
  5. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    530
    I'll ask again. What error? I don't see where you would state what the error is. Did you forgot to copy paste it? Or maybe forum didn't like how you added it and it got lost.
     
  6. LynxXd

    LynxXd

    Joined:
    Jan 21, 2023
    Posts:
    6
    oh forgot to add it mbmb ,,Assets\PlayerMovementOk.cs(23,13): error CS0019: Operator '&&' cannot be applied to operands of type 'bool' and 'method group' ,,
     
  7. LynxXd

    LynxXd

    Joined:
    Jan 21, 2023
    Posts:
    6
    upload_2023-1-21_21-26-11.png also i get this under the script
     
  8. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    530
    The error message tells where the problem is or at least rough area. \PlayerMovementOk.cs(23,13) means line 23 position 13 . If you clicked on the error in Unity (don't remember single or double) it should open the corresponding position in editor.

    Now go back to tutorial and carefully compare this line with the code in tutorial. Your code is not exactly the same as tutorial. And the compiler is even telling where the problem is so you don't have to check all of it.
     
    LynxXd likes this.
  9. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    491
    You have just made a typo.
    On this line
    if (Input.GetButtonDown("Jump") && IsGrounded)
    you are using IsGrounded as if it is a straight boolean variable, however it is actually a method and as such you should change the line to be
    if (Input.GetButtonDown("Jump") && IsGrounded())
    which then uses the return value from that method. You can see this at 3:54 in your tutorial video.

    That is expected as it is just telling you that this script has an error and you have to fix it before Unity will let you continue work.

    Just one other point for the future:
    Typing all in caps (which I'm glad to see you stopped doing) is equivalent to yelling on a forum. Some people will immediately start ignoring posts like that as it is not great etiquette.
     
  10. LynxXd

    LynxXd

    Joined:
    Jan 21, 2023
    Posts:
    6
    oh thx, got the problem fixed thx a lot guys !!! appreciate it a lot !!! thx a lot again