- Home /
Problem with regeneration script
I know this is probably a very simple problems, however I'm having trouble thinking up why it's wrong.
public int health = 50;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(health < 100) {
health += 1 * Time.deltaTime * 3;
}
}
Now this is supposed to regenerate player health over time the only problem is, I'm getting this error when I include the Time.deltaTime bit.
Assets/Assets - scripts/Damage.cs(15,25): error CS0266: Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?)
As I said before I'm sure this is just something simple that I'm missing so thanks for helping!
Comment
Best Answer
Answer by KellyThomas · Dec 23, 2013 at 03:06 PM
In c# you can cast from one type to another with the following syntax:
(type)value
Or in your case:
health += (int)(1 * Time.deltaTime * 3);
Note that this will truncate the float.
For more fine grained control you can use Mathf
functions: `CeilToInt()`, `FloorToInt()` or `RoundToInt()`.