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. Dismiss Notice

Cannot implicity convert type 'float' to 'bool'

Discussion in 'Editor & General Support' started by Jesper2908, May 30, 2021.

  1. Jesper2908

    Jesper2908

    Joined:
    Feb 28, 2021
    Posts:
    6
    Hi there! I am kinda new here and i got this error. Can anyone help me with this?
    Code:


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    public class HealthBar : MonoBehaviour
    {
    public Slider slider;
    public Gradient gradient;
    public Image fill;
    private float deth;
    public void SetMaxHealth(int health)
    {
    slider.maxValue = health;
    slider.value = health;
    fill.color = gradient.Evaluate(1f);
    }
    public void SetHealth(int health)
    {
    slider.value = health;
    fill.color = gradient.Evaluate(slider.normalizedValue);
    deth = slider.value = 0f;
    }
    public void update(int health)
    {
    if (deth)
    {
    Debug.Log("you died");
    }
    }
    }

    Greethings Jesper!
     
  2. BenniKo

    BenniKo

    Joined:
    Mar 24, 2015
    Posts:
    100
    Please use code tags. Then please also post the complete error message because it shows on which line the error occurs.
    In your case i think it's the following line
    Code (CSharp):
    1. if(deth)
    You can't use that with a float.
    In your case you might be able to use
    Code (CSharp):
    1. if (Mathf.Approximately(deth, 0.0f))
    But in general a better logic would be to define
    Code (CSharp):
    1. bool dead = false;
    And then in your setHealth function
    Code (CSharp):
    1. if(health <= 0) {
    2. dead = true;
    3. }
    Then you can use it as a bool in the update function
    Code (CSharp):
    1. if(dead) {
    2. }
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
  4. Jesper2908

    Jesper2908

    Joined:
    Feb 28, 2021
    Posts:
    6
    Hi there BenniKo
    I now have no more errors! Thx for that!
    But now it doesn't do the Debug.Log thing when 0 Health is reached can you help me?
    Code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    public class HealthBar : MonoBehaviour
    {
    public Slider slider;
    public Gradient gradient;
    public Image fill;
    bool dead = false;
    public void SetMaxHealth(int health)
    {
    slider.maxValue = health;
    slider.value = health;
    fill.color = gradient.Evaluate(1f);
    }
    public void SetHealth(int health)
    {
    slider.value = health;
    fill.color = gradient.Evaluate(slider.normalizedValue);
    }
    public void die(int health)
    {
    if (health <= 0)
    {
    dead = true;
    }
    if (dead)
    {
    Debug.Log("you died");
    }
    }
    }
     
  5. mishakozlov74

    mishakozlov74

    Joined:
    Aug 8, 2018
    Posts:
    133
    BenniKo gave you two good advices and you ignored both of them. If you want to get help on the forum, please respect others. It's hard to understand your code without syntax highlight.
     
    Kurt-Dekker likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    If you cannot help yourself by adding code tags, I'm afraid you're on your own. You've been asked a few times. It's not hard. It's just a website. Don't be stubborn. Use code tags.
     
  7. Jesper2908

    Jesper2908

    Joined:
    Feb 28, 2021
    Posts:
    6
    Hi there! Im so so sorry. I din't mean to direspect someone.
    Could you please explane a little what code syntax highlight is?

    Im new...
    My apologie!
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Remember, nobody here can read your mind.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    The important parts of an 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)

    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.

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855
     
  9. Jesper2908

    Jesper2908

    Joined:
    Feb 28, 2021
    Posts:
    6
    This will work then i hope! Thx for helping me with that!
    I now don't have any more error's but the Debug.Log code at line 31.
    Again my apologies for earlier.


    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6. public class HealthBar : MonoBehaviour
    7. {
    8. public Slider slider;
    9. public Gradient gradient;
    10. public Image fill;
    11. bool dead = false;
    12. public void SetMaxHealth(int health)
    13. {
    14. slider.maxValue = health;
    15. slider.value = health;
    16. fill.color = gradient.Evaluate(1f);
    17. }
    18. public void SetHealth(int health)
    19. {
    20. slider.value = health;
    21. fill.color = gradient.Evaluate(slider.normalizedValue);
    22. if(health <= 0)
    23. {
    24. dead = true;
    25. }
    26. }
    27. public void update(int health)
    28. {
    29. if (dead)
    30. {
    31. Debug.Log("you died");
    32. }
    33. }
    44.}
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Are you calling this? I don't see it in the above code.

    Generally stay away from semi-duplicate names like "update" since Unity already implements an Update() method (zero parameters).

    dead
    is also private, and the only line that sets it true is line 24. Is that line running?

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    Honestly, if you're just making a health bar that also sets the player dead, there's a lot of code going on here that is kinda not really necessary. You might want to check out some basic tutorials on how health and death are typically tracked.
     
    BenniKo likes this.
  11. Jesper2908

    Jesper2908

    Joined:
    Feb 28, 2021
    Posts:
    6
    Im getting this (See screenshot and the code last codes i sended)

    Does this has to do anything with it? And can you help me if so fix it?
     

    Attached Files:

  12. Jesper2908

    Jesper2908

    Joined:
    Feb 28, 2021
    Posts:
    6
    Hi there!
    I would love to say that everything is working!
    I want to thank you all and have a nice day/night!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class HealthBar : MonoBehaviour
    8. {
    9.  
    10.     public Slider slider;
    11.     public Gradient gradient;
    12.     public Image fill;
    13.  
    14.     bool dead = false;
    15.  
    16.     public void SetMaxHealth(int health)
    17.     {
    18.         slider.maxValue = health;
    19.         slider.value = health;
    20.  
    21.         fill.color = gradient.Evaluate(1f);
    22.     }
    23.  
    24.     public void SetHealth(int health)
    25.     {
    26.         slider.value = health;
    27.  
    28.         fill.color = gradient.Evaluate(slider.normalizedValue);
    29.  
    30.         if(health <= 0)
    31.         {
    32.             dead = true;
    33.             SceneManager.LoadScene("startmenu");
    34.         }
    35.     }
    36.  
    37.     void update(int health)
    38.     {
    39.        
    40.     }
    41. }
    42.  
     
    mishakozlov74 likes this.