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

Float and Int need help!

Discussion in 'Scripting' started by liamVAgamessss, Apr 14, 2020.

  1. liamVAgamessss

    liamVAgamessss

    Joined:
    Apr 11, 2020
    Posts:
    13
    Hi im following the tutorial to attack enemies with this tutorial

    and the Health(bar) system from this tutorial


    But in the Attack Enemies video he uses after public "float" but in the Health Bar system he uses "Int" my scripts don't match.. I wonder if there is a word that combines float and int so i can use it both. If it depends on my scripts here they are:
    Enemy Healthbar (Enemyhealth) :
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Enemyhealth : MonoBehaviour
    {
    public int maxHealth = 50;
    public int currentHealth;

    public HealthBar healthBar;

    public void TakeDamage (float amount)
    {
    currentHealth -= amount;
    if (currentHealth <= 0f)
    {
    Die();
    }
    }

    void Die()
    {
    Destroy(gameObject);
    }

    // Start is called before the first frame update
    void Start()
    {
    currentHealth = maxHealth;
    healthBar.SetMaxHealth(maxHealth);
    }

    public void TakeDamage(int damage)
    {
    currentHealth -= damage;
    healthBar.SetHealth(currentHealth);
    }

    }


    Attack Enemy (Gun1) :

    using UnityEngine;

    public class Gun1 : MonoBehaviour
    {
    public float damage = 10f;
    public float range = 100f;

    public Camera fpsCam;

    // Update is called once per frame
    void Update() {

    if (Input.GetButtonDown("Fire1"))
    {
    Shoot();
    }

    }

    void Shoot ()
    {
    RaycastHit hit;
    if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
    {
    Debug.Log(hit.transform.name);

    Enemy target = hit.transform.GetComponent<Enemy>();
    }
    }
    }


     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You can convert between float and int by casting, but there isn't some kind of floatint type that combines both. This is because floats and ints are stored fundamentally differently in memory. Integers are stored exactly as the intended value, while floats are stored approximately within the level of precision the float type is capable of.

    But here is an example of converting between these two types:
    Code (csharp):
    1. float myFloat = 20.7f;
    2. int myInt = (int)myFloat;
    3. Debug.Log(myInt.ToString());
    The above creates a float of 20.7 and an int, then assigns the int the casted integer value of the float. Since ints are incapable of storing values following a decimal point, the Debug.Log statement will output the value of the int as "20".

    Code (csharp):
    1. int myInt = 20;
    2. float myFloat = (float)myInt;
    3. Debug.Log(myFloat.ToString());
    The above does the reverse, and the Debug.Log should output something like "20.0".
     
    Yoreki likes this.
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    I believe you are confusing something. The terms 'float' and 'int' are types. They tell the system what type the variable stored there is of. An int(eger) is a whole number, while a float(ing point) number is a decimal number. Decide on one, based on what type your health should be in your game and then use that. One of your tutorials decided that health is a floating point number, while the other wanted it to only be a whole number.

    Also, please use code tags to post code examples :)
    Edit: @Joe-Censored you were a bit quicker!
     
    Joe-Censored likes this.
  4. liamVAgamessss

    liamVAgamessss

    Joined:
    Apr 11, 2020
    Posts:
    13
    Ah okay, thanks!
     
  5. liamVAgamessss

    liamVAgamessss

    Joined:
    Apr 11, 2020
    Posts:
    13
    But what do you prefer to write in my script and should i write it in the Healthbar script or the Attackenemy(gun) script?
     
  6. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    It, again, depends on your game. For more complex games, like RPGs, where a lot of complex calculations are involved, like armor, resistances, different damage types, multipliers, crits and so on, you will want to use floats. If you have something like health regeneration over time, you will want to use floats as well since it makes it easier to add a fraction of a whole number per frame. For more simple games where you dont intend to go between full values (think games where the HP is represented by hearts for example, and where you collect health as drops), you can easily go with integers.

    Imho, i would go with floats most of the time. It makes your life easier.
    But in the end it depends on the design of your game.
     
  7. liamVAgamessss

    liamVAgamessss

    Joined:
    Apr 11, 2020
    Posts:
    13
    So what should i put in my script if i want to make it a float?

    This is my error tho, Assets\Scripts\Enemyhealth.cs(14,9): error CS0266: Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?)

    (Thanks alot for your help bytheway!)
     
  8. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Make currentHealth, maxHealth and the parameter of the TakeDamage() method a float instead of an int.

    The error is basically just telling you that the compiler does not want to convert a float to an int by itself. As @Joe-Censored explained in his post, there would be a loss involved. If you had a float of 20.7 and wanted to put it into some integer variable (or in your case, subtract it from an integer), there would be a loss involved, since the floating point number 20.7 cut down into an integer, would only be 20. You can still tell the compiler to do this (which basically tells it that this is indeed intended to happen) by using casts, as was also explained by Joe. The other way, ie substracting an int value from a float value, is fine by the way, since there is no loss involved in converting an int value to a float.
     
  9. liamVAgamessss

    liamVAgamessss

    Joined:
    Apr 11, 2020
    Posts:
    13
    So im a total noob with coding so what should i change exactly? Thank you for your time and effort btw!
     
  10. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    You replace 'int' with 'float'. Without wanting to sound rude tho, if you are having problems with basics such as what a type is, then you really should not worry about getting your health / damage system to work, but instead work on improving your basics as this will save you tremendous amounts of time. Sebastian Lague also did a great tutorial series on Unity + C# on youtube which you may want to look into.
     
    Joe-Censored likes this.
  11. liamVAgamessss

    liamVAgamessss

    Joined:
    Apr 11, 2020
    Posts:
    13
    Okay thanks man! I still got some errors (that have nothing to do with int and float tho) so i will check Sebastian Lague out! Thanks for your help!
     
  12. YouWhoDrew

    YouWhoDrew

    Joined:
    Jul 13, 2020
    Posts:
    1
    @liamVAgamessss if you ever got it working please send it to me cause I am having the same issues
     
  13. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    They were last seen in 2020 on the forums. You'd be better off making your own post, describing your problem, showing any relevant code and errors then necroing a post that is almost 3 years old.
     
    Bunny83 and Kurt-Dekker like this.