#I cant convert int to float

32 messages · Page 1 of 1 (latest)

ruby ridge
#

i want the GameOverScreen to be active if my health is <= to 0 but i dont know how to convert it

proven nova
#

[]cb

unreal waspBOT
#

Use codeblocks to send code in a message!

To make a codeblock, surround your code with ``` (3 backticks. Click here to see where the key is)

To use syntax highlighting, add the file extension of the language you wish to highlight (cs for C#, cpp for C++)

For example:
```cs
Console.WriteLine("Hello World");
```

Produces:

Console.WriteLine("Hello World");

To send lengthy code, paste it into https://paste.myst.rs/ and send the link of the paste into chat.

ruby ridge
#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class GameOverScreen : MonoBehaviour
{
    public CoinManager coinCount;
    public Text pointsText;
    public PlayerHealth currentHealth;
    float CurrentHealth = currentHealth;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (CurrentHealth <= 0)
        {
            gameObject.SetActive(true);
            pointsText.text = coinCount.ToString() + "Points";

        }
    }
}
#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerHealth : MonoBehaviour
{
    public Text healthText;
    public int maxHealth = 3;
    public int currentHealth;

    

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

    // Update is called once per frame
    void Update()
    {
        healthText.text = ": " + currentHealth.ToString();
    }

   


}

honest creek
#

Explain what this line is supposed to do:
float CurrentHealth = currentHealth;.

ruby ridge
#

i tried to convert the currentHealth variant (which is an int) to float with the sources i found off internet but it didnt work, i forgot to delete it before posting

#

sorry

fervent knoll
#

You miss the point of the question

valid depot
#

In the first code chunk it looks like your trying to store the script "PlayerHealth" in the float field "CurrentHealth" which isn't something you can do

valid depot
#

there are many different ways to change data types such as

  • Type casting
  • Parsing
  • Convert
  • Manipulating the int with a float to produce a float number

to name a few

fervent knoll
#

None of these are necessary

#

You can store an int value in a float perfectly fine

#

i.e. someFloatVariable = intValue

valid depot
#

can you?

fervent knoll
#

yes

#

Why wouldn't you be able to var

#

It's not a narrowing conversion

valid depot
#

pretty sure you cant

fervent knoll
#

You can

#

Do you want proof kekw

valid depot
fervent knoll
#

That's the other way

#

float to int

#

OP specifically asked for int to float

valid depot
#

aw my bad

fervent knoll
#

float to int does require a cast, because it is a narrowing conversion

#

(namely, you lose the fractional component)

valid depot
#

thats where i was confused

honest creek
#

What actually confused me was that the error described in the post's title is definitely not the same error that's present in OP's console/code. LUL

proven nova
#

Yeah