Search Unity

SOLVED: error cs0019: Operator `>' cannot be applied to operands of type `method group' and `double'

Discussion in 'Scripting' started by ContraXGaming, Sep 24, 2018.

Thread Status:
Not open for further replies.
  1. ContraXGaming

    ContraXGaming

    Joined:
    Dec 15, 2017
    Posts:
    78
    Hi, well I'm trying to make move with animation when i hit the left and right button. so when I'm putting a method inside UpdateInput it shows me this error
    "error cs0019: Operator `>' cannot be applied to operands of type `method group' and `double'"
    .. the problem is that i dont know how could i write it in here or should i convert it. if anyone knows how, thanks.

    Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Chara : MonoBehaviour {
    6.  
    7.     private float btnHorizontal;
    8.     public float horizontalForce;
    9.     protected bool facingRight;
    10.     protected float direction;
    11.     protected bool move;
    12.     protected Animator animator;
    13.     public Rigidbody2D myBody { get; set; }
    14.     public float health = MAX_HEALTH;
    15.     public string fighterName;
    16.     public Fighter oponent;
    17.     public GameObject Target;
    18.     public GameObject EnemysTarget;
    19.     private Vector3 v_diff;
    20.     private float atan2;
    21.  
    22.  
    23.     public bool enable;
    24.  
    25.     private static Chara instance;
    26.  
    27.     public static float MAX_HEALTH = 100f;
    28.  
    29.     void Start ()
    30.     {
    31.         myBody = GetComponent<Rigidbody2D> ();
    32.         animator = GetComponent<Animator> ();
    33.     }
    34.  
    35.     public void UpdateInput()
    36.     {
    37.         if (Input.GetAxisRaw ("Horizontal") > 0.1 && !facingRight || Input.GetAxisRaw ("Horizontal") < -0.1 && facingRight) {
    38.             animator.SetBool ("WALK", true);
    39.         } else {
    40.             animator.SetBool ("WALK", false);
    41.         }
    42.  
    43.         if (Input.GetAxisRaw ("Horizontal") < -0.1 && !facingRight || Input.GetAxisRaw ("Horizontal") > 0.1 && facingRight) {
    44.             animator.SetBool ("WALK_BACK", true);
    45.         } else {
    46.             animator.SetBool ("WALK_BACK", false);
    47.         }
    48.  
    49.    
    50.         if (BtnMove > 0.1 && !facingRight || BtnMove < -0.1 && facingRight) {
    51.             animator.SetBool ("WALK", true);
    52.         } else {
    53.             animator.SetBool ("WALK", false);
    54.         }
    55.  
    56.         if (BtnMove < -0.1 && !facingRight || BtnMove > 0.1 && facingRight) {
    57.             animator.SetBool ("WALK_BACK", true);
    58.         } else {
    59.             animator.SetBool ("WALK_BACK", false);
    60.         }
    61.  
    62.     }
    63.  
    64.     public void ChangeDirection()
    65.     {
    66.         facingRight = !facingRight;
    67.         transform.localScale = new Vector3 (transform.localScale.x * -1, 1, 1);
    68.     }
    69.  
    70.     void Update () {
    71.  
    72.         EnemyLookAtTarget ();
    73.         LookAtTarget ();
    74.         UpdateInput ();
    75.     }
    76.  
    77.  
    78.  
    79.     void FixedUpdate()
    80.     {
    81.         float horizontal = Input.GetAxisRaw ("Horizontal");
    82.  
    83.         if (move)
    84.         {
    85.             this.btnHorizontal = Mathf.Lerp (btnHorizontal, direction, Time.deltaTime * 2);
    86.             HandleMovement (direction);
    87.         }
    88.         else
    89.         {
    90.             HandleMovement(horizontal);
    91.         }
    92.     }
    93.  
    94.     public void HandleMovement(float horizontal)
    95.     {
    96.         myBody.velocity = new Vector2 (horizontal * horizontalForce, myBody.velocity.y);
    97.     }
    98.  
    99.     private void EnemyLookAtTarget()
    100.     {
    101.         if (EnemysTarget != null)
    102.         {
    103.             float xDir = EnemysTarget.transform.position.x - transform.position.x;
    104.  
    105.             if (xDir < 0 && facingRight || xDir > 0 && !facingRight)
    106.             {
    107.                 ChangeDirection ();
    108.             }
    109.         }
    110.     }
    111.  
    112.     private void LookAtTarget()
    113.     {
    114.         if (Target != null)
    115.         {
    116.             float xDir = Target.transform.position.x - transform.position.x;
    117.  
    118.             if (xDir < 0 && !facingRight || xDir > 0 && facingRight)
    119.             {
    120.                 ChangeDirection ();
    121.             }
    122.         }
    123.     }
    124.  
    125.     public void BtnMove(float direction)
    126.     {
    127.         this.direction = direction;
    128.         this.move = true;
    129.     }
    130.  
    131.     public void BtnStopMove()
    132.     {
    133.         this.direction = 0;
    134.         this.btnHorizontal = 0;
    135.         this.move = false;
    136.     }
    137. }
     
    Last edited: Sep 24, 2018
  2. mjzx

    mjzx

    Joined:
    Aug 18, 2013
    Posts:
    114
    You got that error because of these lines:
    Code (CSharp):
    1. if (BtnMove > 0.1 && !facingRight || BtnMove < -0.1 && facingRight) {
    And
    Code (CSharp):
    1. if (BtnMove < -0.1 && !facingRight || BtnMove > 0.1 && facingRight) {
    The reason for that error is because you are comparing a void (BtnMove) with a value (0.1 and -0.1)
    Not sure what you were trying to do there.

    Also, when using decimal numbers such as 0.1, its needs to be written as 0.1f. (With the letter 'f' on the end.)
     
  3. ContraXGaming

    ContraXGaming

    Joined:
    Dec 15, 2017
    Posts:
    78
    Yeah, i know why i get error. But i want to know is there any way to convert the method and write it inside without getting error.
     
    Last edited: Sep 24, 2018
  4. mjzx

    mjzx

    Joined:
    Aug 18, 2013
    Posts:
    114
    Well, what value do you want to compare that's related to BtnMove?
    'direction'? or 'move'?
    Code (CSharp):
    1. public void BtnMove(float direction)
    2.     {
    3.         this.direction = direction;
    4.         this.move = true;
    5.     }
     
  5. ContraXGaming

    ContraXGaming

    Joined:
    Dec 15, 2017
    Posts:
    78
    I would want to know for both of them.
     
  6. mjzx

    mjzx

    Joined:
    Aug 18, 2013
    Posts:
    114
    Do you mean something like this?
    Code (CSharp):
    1. if ((this.direction > 0.1 && this.move) && !facingRight || (this.direction < -0.1 && this.move) && facingRight) {
     
  7. ContraXGaming

    ContraXGaming

    Joined:
    Dec 15, 2017
    Posts:
    78
    yes, thank you. Now the problem is solved.
     
  8. cooldoggaming5501

    cooldoggaming5501

    Joined:
    Aug 25, 2021
    Posts:
    21
    i have a problen in line 25
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3.  
    4. [RequireComponent(typeof(PlayerMotor))]
    5. public class PlayerController : MonoBehaviour
    6. {
    7.  
    8.     [SerializeField]
    9.     private float speed = 5f;
    10.  
    11.     private PlayerMotor motor;
    12.     private Vector3 _zMov;
    13.  
    14.     void Start()
    15.     {
    16.         motor = GetComponent<PlayerMotor>();
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         //Calculate movement velocity as a 3D vector
    22.         float _xMov = Input.GetAxisRaw("Horizontal");
    23.         float _zMove = Input.GetAxisRaw("Vertical");
    24.  
    25.         Vector3 _movVertical = transform.forward * _zMov;
    26.         Vector3 _movHorizontal = transform.right * _xMov;
    27.  
    28.         // Final movement vector
    29.         Vector3 _velocity = (_movHorizontal + _movVertical).normalized * speed;
    30.     }
    31.  
    32.  
    33.  
    34.  
    35.  
    36.  
    37. }
     
  9. cooldoggaming5501

    cooldoggaming5501

    Joined:
    Aug 25, 2021
    Posts:
    21
    nvm i found the problem
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    Glad you sorted it but don't hijack/necro threads please, create your own.
     
Thread Status:
Not open for further replies.