#I cant convert int to float
32 messages · Page 1 of 1 (latest)
[]cb
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.
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();
}
}
Explain what this line is supposed to do:
float CurrentHealth = currentHealth;.
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
You miss the point of the question
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
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
None of these are necessary
You can store an int value in a float perfectly fine
i.e. someFloatVariable = intValue
can you?
pretty sure you cant
aw my bad
float to int does require a cast, because it is a narrowing conversion
(namely, you lose the fractional component)
thats where i was confused
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. 
Yeah

