- Home /
Static Variable crashes when accessed too fast?
Inspired by UnityCoder website, i converted a Sin Cos lookuptable code that is 10 times faster than Mathf.Sin, it worked at 10k calls per frame, until i tried to call the same array value twice in the same loop, in which case it chrashes and sais: IndexOutOfRangeException: Array index is out of range. m.cos (Single rr) (at Assets/-=TubeGlider/0N/Scripts/m.js:27)
m.sin works fine and m.cos causes error when i ask the same m.cos value twice in a frame in start function.
m.JS -
//Fast Sin Cos lookup table helper functions accessible by static functions.
private static var lookupTableSize : int=8192; // or 1024//16384
private static var mSin: float[];//
private static var mCos: float[];
private static var mconst: float;
function Awake ()
{
// init arrays
mCos = new float[lookupTableSize];
mSin = new float[lookupTableSize];
mconst = lookupTableSize / (2*Mathf.PI); // array index constant
// build lookup tables
var angle:float = 0;
for (var i:int=0;i<lookupTableSize;i++)
{
mSin[i] = Mathf.Sin(angle);
mCos[i] = Mathf.Cos(angle);
angle += 2*Mathf.PI/lookupTableSize;
}
}
// helper functions
static function sin (rr:float) :float {return mSin[(rr * mconst)%lookupTableSize];}
static function cos (rr:float) :float {return mCos[(rr * mconst)%lookupTableSize];}
Your answer
Follow this Question
Related Questions
cannot be assigned to (it is read-only) Problem 2 Answers
An instance of type "x" is required to access non-static variable "y" 2 Answers
An object reference is required to access non-static member `UnityEngine.Component.transform' 1 Answer
Trying to create a static class for WWW-based functions and such 0 Answers
Unity how to access a static variable from the FirstPersonController component 2 Answers