The Int Division Problem - Unity Edition (Solved)

:shock: Noob Crisis: Averted! (again) :shock:

I’m familiar with the Integer division problem from my introductory computer science courses. What I’m not familiar with is how to neutralize them in Unity. I’m using the following code for a modular health bar:

@script AddComponentMenu( "GUI/HP Meter" )

var yourX : int;
var yourY : int;
var yourWidth : int;
var yourHeight : int;
var meterBase : Texture2D;
var meterBar : Texture2D;

var me: player;

var meterBaseRect;
var meterBarRect;
var meterBaseHeight;
var meterBarHeight;
var meterLength;
var meterMaxLength;

function Start()
{
  me = FindObjectOfType( player );
  
  meterMaxLength = yourWidth;
  meterLength = ( me.hp / me.maxhp * 1.0 ) * meterMaxLength;
  print( "Player stats: " + me.hp + "/" + me.maxhp + " -- Evaluates to " + ( me.hp / me.maxhp * 1.0 ) );
  //print( "meterLength: " + meterLength );

  meterBaseHeight = yourHeight;
  meterBarHeight = yourHeight;

  meterBaseRect = Rect( yourX,yourY, meterMaxLength,meterBaseHeight );
  meterBarRect = Rect( yourX,yourY, meterLength,meterBarHeight );
}

function OnGUI()
{
  GUI.DrawTexture( meterBaseRect, meterBase );
  GUI.DrawTexture( meterBarRect, meterBar );
}

function Update()
{
  if( Input.GetKeyDown( KeyCode.D ) )
  {
    me.hp--;
	meterLength = ( me.hp / me.maxhp * 1.0 ) * meterMaxLength;
	print( "Player stats: " + me.hp + "/" + me.maxhp + " -- Evaluates to " + ( me.hp / me.maxhp * 1.0 ) );
	//print( "meterLength: " + meterLength );
	
    meterBarRect = Rect( yourX,yourY, meterLength,meterBarHeight );
  }
}

The debug bar at the bottom of the screen shows:

As you can see, I’ve been trying to use the C+±style workaround of adding the double/float 1.0 to the calculation to try to get it to passively cast the integer division as a double division. While this could be easily solved by simply switching the integer values in the player script object to doubles in code, what tools exist to accomplish what I’m trying to here?

Order of operations work from left to right. So, this statement:

4/5*1.0

will evaluate like:

4 / 5 = 0
0 * 1.0 = 0

You would want to stick the 1.0 at the front of the statement:

1.0*4/5

Which evaluates like:

1.0 * 4 = 4.0 <–float
4.0 / 5 = 0.8

Noob alert indeed - I can’t believe I overlooked the order of operations!

@script AddComponentMenu( "GUI/HP Meter" )

var yourX : int;
var yourY : int;
var yourWidth : int;
var yourHeight : int;
var meterBase : Texture2D;
var meterBar : Texture2D;

var me: player;

var meterBaseRect;
var meterBarRect;
var meterBaseHeight;
var meterBarHeight;
var meterLength;
var meterMaxLength;

function Start()
{
  me = FindObjectOfType( player );
  
  meterMaxLength = yourWidth;
  meterLength = ( 1.0 * me.hp / me.maxhp ) * meterMaxLength;

  meterBaseHeight = yourHeight;
  meterBarHeight = yourHeight;

  meterBaseRect = Rect( yourX,yourY, meterMaxLength,meterBaseHeight );
  meterBarRect = Rect( yourX,yourY, meterLength,meterBarHeight );
}

function OnGUI()
{
  GUI.DrawTexture( meterBaseRect, meterBase );
  GUI.DrawTexture( meterBarRect, meterBar );
}

function Update()
{
  if( Input.GetKeyDown( KeyCode.D ) )
  {
    me.hp--;
    meterLength = ( 1.0 * me.hp / me.maxhp ) * meterMaxLength;
	
    meterBarRect = Rect( yourX,yourY, meterLength,meterBarHeight );
  }
}

All fixed, thanks for the reminder Mike!

I’m having some issues like this i need help