Making a trivial modification to code causes the game to crash. Sometimes Unity also crashes.
Here is the crash:

Note that double-clicking doesn’t bring up the execution point, and there is no stack trace. It appears to be an internal error.
Here is the code:
void FeedValues( float[] x, float voltage, Color inColor )
{
int N = x.Length;
// wave will be display between radius 1/a and a ( times the default cursor ring size )
float a = m_maxAmplitude * voltage; // waveAmplitude
float waveThickness = m_particleSize * voltage * voltage * voltage + 0.1f;
for( int i=0; i < N; i++ )
{
float xi = x[ i ];
xi = Mathf.Clamp( xi, -1f, +1f );
// xi in [-1, 1] gives r in [a^-1, a^+1] = [1/a, a]
float r = 1.0f + a * xi;
float r2 = Mathf.Pow( a, xi );
grains[ i ].position = r2 * defaultPosition[ i ]; // changing r to r2 causes crash
grains[ i ].size = waveThickness;
grains[ i ].color = inColor;
}
if( particleSystem == null )
{
Debug.Log ( "particleSystem == null", this );
Debug.Break( );
}
particleSystem.SetParticles( grains, N );
}
Clamping xi between -1 and +1 doesn’t change anything.
Checking r2 for NaN, it is always valid
Note that the problem can’t be in Mathf.Pow as that executes the same regardless.
The code just repositions a couple of hundred particles each Update().
Strangely I’ve run into exactly the same bug in a different part of my project: in another component’s Update() I do:
float gap = Pi_VecMod( m_chroma_smooth, chroma, 12f );
if( Mathf.Abs( gap ) < 0.5f ) // within half a toneme
m_chroma_smooth = Pi_LerpMod( m_chroma_smooth, chroma, 12f, 0.15f );
else
m_chroma_smooth = chroma;
and get exactly the same crash. If I comment out the code and simply set ‘m_chroma_smooth = chroma;’ regardless, the program now runs fine.
Here is the body code for these functions:
// points A,B on circle length N
// returns shortest-magnitude-1D-vector from A to B
// so if A=1, B = 11, returns -2
float Pi_VecMod( float A, float B, float N )
{
float V = Pi_Repeat( B - A, N ); // Mathf.Repeat
if( V > N / 2f )
V -= N;
return V;
}
float Pi_LerpMod( float src, float dest, float N, float lamda )
{
float v = Pi_VecMod( src, dest, N );
float x = src + lamda * v;
// Debug.Log( "" + x + "," + N );
// Debug.Break();
return Pi_Repeat( x, N );
//return dest;
}
float Pi_Repeat( float _x, float N )
{
float x = _x;
while( x < 0 )
x += N;
while( x >= N )
x -= N;
return x;
}
I actually got this problem first, so as you can see I’ve experimented with writing my own implementation of Mathf.Repeat(…)
This makes me wonder if this whole problem is CPU related, maybe the extra cycles generally the crash. But that makes no sense at all. it is really light processing. It is not going to increase the load by more than 1%.