I obviously didn't want to just make a build for the Mac in the middle of a sprint, so I was holding out until the end of a good sprint to make a build for Mac. Today was the end of said sprint and I went into Unity to do some quick tweaks on a health bar thing. I started the game up and....the damn bug was back! IndexOutOfBounds Exception at the same line of code! I decided I was going to get to the bottom of this since I had promised a build for so many Mac users.
Really quick, the bug happens during an effect that takes place while the player squeezes the right trigger on the controller, or presses return on the keyboard. The controller's trigger provides a high precision range between 0 and 1 depending on how far down the trigger is squeezed. 0 if fully released, 1 if fully squeezed. The return key is just 1 if return key is down and 0 if return key is untouched.
The line of code that was getting the error had to instance where it was doing array indexing, so I had to figure out which array it was. One of the arrays was the materials of a trailRenderer and the other was an array of ColorKeys in a Unity Gradient. I set excessive null and OutOfBounds checkers prior to the index accessing and nothing seemed to fix it. It was simply a very bizarre bug that I assumed came down to some kind of Unity Compiler error.
However, in my numerous tests trying to debug this thing, I discovered the only time this error occurred was when I was using the controller (as opposed to the keyboard) for the particular effect. I thought that was strange. I use a wireless Xbox 360 controller with an Xbox 360 wireless USB adapter and the TattieBogle driver on Mac OS X 10.10 Yosemite. Perhaps this was unique to the controller or the driver. So I tried my teammates PS4 controller (which amazingly works natively with Mac OS X - just plug n' play). That wasn't it.
I went into our code for our InputManager that will return the correct input based on OS since the Joystick controller Axes vary between OSes. Where the code gets the Right Trigger for the Mac joystick controller, I had it print the value of the Right Trigger (typically a number between 0 and 1 as mentioned earlier). I launched the game, the initial value was 0, as expected. But as I squeezed the trigger the values that were printed ranged from -0.99 to 0.99. Ah ha! The Mac right trigger has some goofy range from -1 to 1. This does matter because the amount our swarm units move around depends on the value returned from the right trigger which was assumed to be a number between 0 and 1 but on Mac was returning negative numbers.
The fix was simple. If the right trigger value is less than 0, add 1. This will guarantee it to stay positive and be a number between 0 and 1. This bug was the devil, and I conquered it.
If you have a Mac running...probably any 64-bit version of OS X which I think is everything 10.6 and up, there is now a build for you. Head on over to the Downloads page to grab the latest copy.
Remember that it's recommended to use a joystick controller. We have tested it with Wired Xbox 360 controllers, Wireless Xbox 360 controllers with a wireless USB adapter, and PS4 via MicroUSB. PS4 via bluetooth should also work if you know how to pair your controller with your Mac (hint...System Preferences > Bluetooth...make sure Bluetooth is on and set your PS4 controller to pairing mode).