- Home /
division yields zero
var Columns:float;
private var frameWidth:float=(1.0/Columns);
With the Columns var set to 6 in the inspector this yields Infinity.
var Columns:float=6.0;
private var frameWidth:float=(1.0/Columns);
This is great though. Does setting a float value in the inspector automatically change it to an int? Is the some way to manually cast it back?
Are you sure it's not some other part of an equation that is returning the value infinity?
var Columns : float;
function Start()
{
var frameWidth : float = ( 1.0 / Columns );
Debug.Log( "Columns " + Columns + " : frameWidth " + frameWidth );
}
// Columns 6 : frameWidth 0.1666667
As frameWidth is a private var you cannot see it unless it is playing and you have a Debug.Log showing the outcome of 1.0 / Columns
Yeah, I'm debug.log()'ing right underneath those lines.
Answer by AlucardJay · Sep 16, 2012 at 03:57 AM
Are you sure it's not some other part of an equation that is returning the value infinity?
Yeah, I'm debug.log()'ing right underneath those lines.
In that case, you must be doing it like this :
var Columns : float;
private var frameWidth : float = ( 1.0 / Columns );
function Start()
{
//var frameWidth : float = ( 1.0 / Columns );
Debug.Log( "Columns " + Columns + " : frameWidth " + frameWidth );
}
// Columns 6 : frameWidth 0.1666667 :- this is printed with var inside start function
// Columns 6 : frameWidth Infinity :- this is printed with var outside any function
I'm pretty sure you cannot run an equation outside of a function. That's why the above breaks, but the below works :
var Columns : float;
function Start()
{
var frameWidth : float = ( 1.0 / Columns );
Debug.Log( "Columns " + Columns + " : frameWidth " + frameWidth );
}
// Columns 6 : frameWidth 0.1666667
Yes, all globals are declared and given values at once, not in line-by-line, which means you can't declare a global using another global (consts are the exception, since they aren't really vars.)
In C# you get an error. Javascript is "helpfully" using a temp value of zero for Columns, giving the infinity (1/0.) I wonder if there is't a warning in the OPs compile log.