"int To Float' Implicit Conversion...

Hello, I’m having trouble with converting “Int to Float” vice versa. I’m wondering if someone can help me to convert the int to float stats.currentHealth *= multiplier; It is Underlined and in bold text down below.

using System.Collections.Generic;
using UnityEngine;

public class PowerUp : MonoBehaviour {

public float multiplier = 1.5f;

public GameObject pickupEffect;

void OnTriggerEnter (Collider other)
{
if (other.CompareTag("Player"))
{
Pickup(other);
}
}

void Pickup(Collider player)
{
Instantiate(pickupEffect, transform.position, transform.rotation);

CharacterStats stats = player.GetComponent<CharacterStats>();
stats.currentHealth *= multiplier;

Destroy(gameObject);
}
1 Like

So you want something like

stats.currentHealth = (int)(multiplier * stats.currentHealth);

?

1 Like

You can’t convert float to int implicitly because it will lose fraction part. You may force the conversion:

stats.currentHealth = (int)(stats.currentHealth * multiplier);

But better choice is to use Mathf.RoundToInt, CeilToInt, FloorToInt according to your game logic.

stats.currentHealth = Mathf.RoundToInt(stats.currentHealth * multiplier);
1 Like

Thanks kaarloew for helping me.

Thanks, palex-nx for helping me