Search Unity

error CS0019: Operator '&&' cannot be applied to operands of type 'bool' and 'float'

Discussion in 'Scripting' started by dajewa, Jun 30, 2020.

  1. dajewa

    dajewa

    Joined:
    May 31, 2020
    Posts:
    32
    eh, hey! I got this error when I was trying to make a system that if your close to a guy, a text bubble will appear, then I got this error, so if you can help me I would be very thankful! And also the error appeared at line 21. Thanks! ;)
     
  2. dajewa

    dajewa

    Joined:
    May 31, 2020
    Posts:
    32
    The error was
    error CS0019: Operator '&&' cannot be applied to operands of type 'bool' and 'float'
    forgot to say it...
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    That might be helpful if you actually posted your code.

    But based on the error message, you have a number (or an expression that evaluates to a number) that you were trying to use as if it were a true/false value (that is, a "bool"). Some part of your if statement isn't saying what you meant.
     
  4. dajewa

    dajewa

    Joined:
    May 31, 2020
    Posts:
    32
    whoops, sry I'm really dumb right now here's the code.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class dialogsystem : MonoBehaviour {
    6.  
    7.     public Transform player1;
    8.  
    9.     public Transform player2;
    10.  
    11.     public SpriteRenderer Bubble;
    12.  
    13.    
    14.     // Start is called before the first frame update
    15.     void Start() {
    16.         Bubble = this.gameObject.GetComponent<SpriteRenderer>();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update() {
    21.         if (Vector3.Distance(player1.position, transform.position) == 5f && 4f && 3f && 2f && 1f && 0f && -1f && -2f && -3f && -4f && -5f)  {
    22.             Bubble.enabled = Bubble.enabled;
    23.         } else {
    24.             Bubble.enabled = !Bubble.enabled;
    25.         }
    26.        
    27.     }
    28. }
    29.  
     
  5. Elango

    Elango

    Joined:
    Jan 27, 2016
    Posts:
    107
    You can't do multiple comparison like that:
    if (Vector3.Distance(player1.position, transform.position) == 5f && 4f && 3f && 2f && 1f && 0f && -1f && -2f && -3f && -4f && -5f)

    And even if it were possible you probably mean to use || operator because value cannot be equals multiple different values at the same time...
    Either put matches values in array:
    float[] matches = { 5f, 4f, ... };

    and then compare them:
    if (matches.Contains(Vector3.Distance(player1.position, transform.position)))

    Or make extention:
    Code (CSharp):
    1. public static class Extentions
    2. {
    3.     public static bool EqualsAny<T>(this T val, params T[] values)
    4.     {
    5.         return System.Array.Exists(values, element => element.Equals(val));
    6.     }
    7. }
    and then:
    if (Vector3.Distance(player1.position, transform.position).EqualsAny(5f, 4f, ...))

    And be aware that you unlikely hit true comparing floats. You need you use some tolerance.
    Also Vector3.Distance is absolute so no need to compare negative value.
    Not even sure you mean to use exact values. Maybe you just need range:
    if (Vector3.Distance(player1.position, transform.position) <= 5f)
     
    Last edited: Jun 30, 2020
    StarManta and alexeu like this.
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    I can't even figure out what you're trying to say on line 21, but that's definitely not legal syntax.

    Also note that line 22 does nothing whatsoever; you're setting a variable equal to itself. If line 21 were a valid condition, then you're saying to do nothing at all when it's true, and to make Bubble flicker on and off every frame for as long as it's false.
     
    Joe-Censored and StarManta like this.