Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Enum Passed as Argument Has Wrong Value

Discussion in 'iOS and tvOS' started by John-B, Jun 22, 2014.

  1. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,250
    I'm starting a new thread on this because this problem has turned into something completely different than what I thought at first.

    I have the following enum type:

    Code (JavaScript):
    1. enum PieceType {dNone, dStraight, dCurve, dStair, ... 22 more...}
    When I pass a variable of this type to a function, the function receives the wrong value. In the following code, I can see that the enum variable has the correct value, but when passed as an argument, the function receives the wrong value. This only happens when running on iPad. It always receives the correct value in the Unity editor.

    Code (JavaScript):
    1. debugText.Text = parseInt(PieceType.dNone).ToString(); // displays 0, as it should
    2. theY = GetPieceHeight(thePos, PieceType.dNone);
    3.  
    4. function GetPieceHeight (thePos: Vector3, theType: PieceType): float {
    5.    debugText.Text = parseInt(theType).ToString(); // displays some random integer
    The number that the GetPieceHeight function receives varies, but it's typically a positive or negative 9-digit integer. Sometimes it's 191. And it doesn't matter if I change the function's parameter type to an integer and pass it an integer argument, it still gets the same wrong value.

    One other odd thing. This function is called once on launch, and at that time it receives the correct value. Any time it gets called after that, it receives the wrong value. If I comment out the function call during initialization, it still receives the wrong value when called later on.

    One more thing, I have another function that takes this same enum type as a parameter. It always receives the correct value.

    I seem to have hit a dead end here, so any ideas would be appreciated.
     
  2. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,250
    One more thing, I found that if I comment out all but the first if/then in the function, it works. The function that works looks like this:

    Code (JavaScript):
    1. function GetPieceHeight (thePos: Vector3, theType: PieceType): float {
    2.  
    3.     debugText.Text = parseInt(theType).ToString();  
    4.     var theHeight = 999.0;
    5.     var tPos = thePos;
    6.  
    7.     if (theType == PieceType.dNone) {
    8.         if (Physics.Raycast(tPos, -Vector3.up, hit)) {
    9.             if (hit.distance < theHeight)
    10.                 theHeight = hit.distance;
    11.             debugText.Text = hit.collider.name + " " + hit.distance.ToString("f3");
    12.         }
    13.         else
    14.             debugText.Text = "no hit";
    15.     }
    16.  
    17.     // if/thens that do something different depending on the value of theType are commented out
    18.  
    19.     theHeight = (tPos.y - theHeight) + 0.000005;
    20.     return theHeight;
    21. }
    I'm not using a switch statement because some of the conditions are a bit complex, and a series of if/thens is easier for me to read.

    So something after the first if/then is causing the value of theType to change. How is that possible? The rest of this function looks just like the part that works.
     
  3. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,600
    please create small repro project that shows the problem (like: this function + call it on Start, and print the value or smth) and bug report it
     
  4. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,250
    I'm not sure that would work, but I'll try. Please note that the project must be built and run on iPad for the problem to appear. Works fine in the editor

    One more thing I found: After I commented out the majority of the function, I started adding it back, one if/then at a time. It worked up to a point. Strange thing, it doesn't matter which if/thens I leave/remove from the function, it seems to be the number that are in the function. In other words, I have 26 conditions I need to test for in this function, but if I test for more than 12, it doesn't work. Any 12, doesn't seem to matter. Once I add the 13th if/then back in, it breaks.

    Is there some limit on the number of lines in a function that only applies when the app is built and run on iPad?
     
  5. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,250
    I tried simplifying and eliminating some of the if/thens, no difference. I replaced the multiple if/thens with a switch statement, no difference. I tried everything I could think of.

    What finally worked (looks good so far, anyway), and I have no idea why, is breaking the function up into two functions, the first calling the second.

    The only thing I can figure is that with the switch statement, it looks like there can be no more than 16 cases. The first 16 cases are checked in the original function, then the last 10 cases are checked in the new function. If I add another case to the first switch in the original function, any case, it gives the wrong argument value. But with 16 or less cases in each function, the functions receive the correct argument.

    This makes absolutely no sense to me, but I can't see any other explanation.