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);
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:
float myFloat = 20.7f;
int myInt = (int)myFloat;
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”.
int myInt = 20;
float myFloat = (float)myInt;
Debug.Log(myFloat.ToString());
The above does the reverse, and the Debug.Log should output something like “20.0”.
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!
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.
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?)
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.
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.
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.