I have a charge meter that’s set to a max of 150, when i get to that number i want it to stop at 150 in the inspector rather than adding up to infinite

using UnityEngine ;
public class SunFollower : MonoBehaviour
{
[Range ( 0, 150)]
public float f ;
}
Nope that didnt work its still going up over the limit, i had it set up like this,
i dont want to do >= 150 i want it exactly to stop at 150
public int chargemeter
if (Input.GetMouseButton(0))
{
//Adding 1 every frame
ChargeMeter += 1;
}
if(ChargeMeter == 150)
{
Destroy(gameObject);
}
What was suggested above stops you from setting the value higher than 150 in the inspector. What you’re doing is setting it higher in code, so just check in code whether you should add more or not.
public int chargemeter
if (Input.GetMouseButton(0))
{
//Adding 1 every frame
if (ChargeMeter < 150)
{
ChargeMeter += 1;
}
}
if(ChargeMeter == 150)
{
Destroy(gameObject);
}
2 Likes
It worked i didn’t know i was this close to what i wanted Thank you so much ![]()
1 Like
Just in addition for such scenario, also an alternative is to use Clamp, like
ChargeMeter += 1;
ChargeMeter = Mathf.Clamp ( ChargeMeter, 0, 150 ) ;
But is probably less optimal for given case.
1 Like