Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I see this question is tagged Unity3D.</p> <p>Is it possible that <code>f</code> is a public variable exposing it to be modified in the Unity Editor? If so, any value set in the editor will override a value set in the code during variable declaration. The value set during declaration is simply used as a default value for the Unity Editor.</p> <p>If this is the case then someone could have set <code>f</code> to 0 in the editor causing it to be 0 when you do your math.</p> <hr> <p><strong>Edited Answer After Some Testing: 8/22/2013</strong></p> <p>I was able to repeat your problem when using Unity. It seems there is a bug in Unity that doesn't allow variables to be used during initialization of an array at declaration. Everything works properly if it is a standard C# project, however, so this is related to Unity only.</p> <p>The problem has nothing to do with <code>f</code> being <code>static</code> or <code>readonly</code>, only that it is a variable. The following code does not work:</p> <pre><code>public void init() { float f = 1.25f; float[,] FLIPPER_CENTERS = new float[,] { { (5+f), (27*f) }, { 30 - (20*f), (27*f)}, { (6*f), (25*f) }, { 20 - (6*f), (25*f) }, { (8), (15)}, { (10 - 8), (15)}, { (8), (20)}, { (67 - 8), (20)}, }; Debug.Log(FLIPPER_CENTERS[0,0]); // Outputs 0 | Expected 6.25f } </code></pre> <p>Anywhere the variable <code>f</code> is used in the array initialization will result in a 0. More specifically, it doesn't just set <code>f = 0</code> it sets the whole expression to 0. For example, the first element in the array above, <code>5+f</code>, would result in 0 - not 5. The elements with constants, however, evaluate normally (such as the last few elements). It seems if Unity is bailing out of the evaluation when it encounters a variable.</p> <p>If, however, I define the array in one line and then set the elements later everything works normally. For example:</p> <pre><code>public void init() { float f = 1.25f; float[,] FLIPPER_CENTERS = new float[8, 2]; FLIPPER_CENTERS[0, 0] = 20*f; FLIPPER_CENTERS[0, 1] = 27*f; FLIPPER_CENTERS[1, 0] = 30 - (20*f); FLIPPER_CENTERS[1, 1] = 27*f; FLIPPER_CENTERS[2, 0] = 6*f; FLIPPER_CENTERS[2, 1] = 25*f; FLIPPER_CENTERS[3, 0] = 20 - (6*f); FLIPPER_CENTERS[3, 1] = 25*f; FLIPPER_CENTERS[4, 0] = 8; FLIPPER_CENTERS[4, 1] = 15; FLIPPER_CENTERS[5, 0] = 10 - 8; FLIPPER_CENTERS[5, 1] = 15; FLIPPER_CENTERS[6, 0] = 8; FLIPPER_CENTERS[6, 1] = 20; FLIPPER_CENTERS[7, 0] = 67 - 8; FLIPPER_CENTERS[7, 1] = 20; Debug.Log(FLIPPER_CENTERS[0,0]); // Outputs 25.0f | Expected 25.0f } </code></pre> <p>Just did some further testing and it seems that the problem is only present with multidimensional arrays. The following gives expected output:</p> <pre><code>public void init() { float f = 1.25f; float[] FLIPPER_CENTERS = new float[] { (f), (f * 2), (f * 3), (f * 4), (f + 1), (f + 2), (f + 3), (f + 5) }; Debug.Log(FLIPPER_CENTERS[0]); // Outputs 1.25f | Expected 1.25f } </code></pre> <p>This certainly seems like a bug in Unity to me. It is generally recommended, however, that <code>const</code> be used over <code>static readonly</code>. Using <code>const</code> provides a performance benefit (albeit slight) since it is converted into a literal at compile time. In addition, using <code>const</code> will avoid this problem altogether.</p> <p>I know you already figured out <code>const</code> works, just wanted to give you a little information that I discovered.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload