2017/11 - How I'm Learning Unity - And some "bug" examples

Unity had been, up until this morning, giving me the impression that it is a horribly buggy piece of software. I've come to realise that this isn't as true as I initially thought, and at least some of the blame rests with me. There ARE bugs in Unity, but despite this, I have yet to encounter any. All the "buggy features" I've experienced so far were actually due to my own inexperience with Unity; one really has to be aware of ones actions when developing with it. Knowing how things are supposed to work together in Unity (And some tricks of the trade) would deifnitely help a lot too (My trick of the trade for anyone starting out is for them to use capsules for hit detection. They are the best trade-off in terms of performance-accuracy). In any case, here are some examples of bugs-that-were-not-bugs that I came across in Unity. Afterwards, I give my opinion on the learning-by-doing approach that led me to these bugs:

1. Fire Button Blocked By Text Area

This one was pretty basic, but really opened my eyes on how to think like a game developer (or at least one who is using Unity). A  set of buttons would not respond to clicks, unless the hitmarker for the click was on the right-most side of the button. I researched quite a bit on this, and even found was looked like a bug report on Unity Answers for this issue. Alas, it was actually down to my own usage of an overlaying text area. Despite changing the alpha of the text area to 0, this does not, in fact, stop the text area from being a raycast target. Despite being invisible on-screen, the text area was still there, and still blocking all bu the right-most hit area of the button  indecision

A quick change to just how eligible this text area was for getting in the way of my goddamn buttons soon put an end to its shenanniggns.

2. Multiple Audio Sources

I took my time in upgrading Unity. So much so that when the time came to upgrade, I was quite a bit behind in releases. I noticed that on Unity Answers, a few people had been encountering the problem I mentioned above (Or so I thought). This issue was fixed in the next upgrade, and the next upgrade included Vuforia support too! "Two birds with one stone!", I thought. So I upgraded, fixed a few issues caused by the upgrade, and was on my merry way to Starbucks for more development and coffee-related motivation. I used my earphones to drown out the sounds that other customers were making at the coffee shop, so I didn't notice that there was a pretty big issue with the audio. When I got home, I found that firing the gun in-game no longer produced sound! Worse still, it reset the background music every time it played! This was a REALLY annoying issue, because it really looked as though everything should be working fine. I read through at least 20 different UnityAnswers solutions, but no-one seemed to have experienced my problem yet (Although the answers clarified that you can't play more than one clip on an audiosource at any one time). The code hadn't changed since the upgrade, so this really shouldn't have been happening. Then I thought back to the upgrade... Then the code... Then the upgrade again...

I thought "Well it must have had something to do with the upgrade". But the only thing that really changed was the camer-



The AR camera I was using before was a kind of custom gameobject created for Vuforia support. NOW Unity had it's own Vuforia-specific objects. Looking at the code I used to reference the AudioSource objects:

FindObjectsOfType (typeof(AudioSource)) as AudioSource[];

I remembered that the original Vuforia plugin gameobject I used was not a regular gameobject. It was entirely plausible that it was now being treated as a regular gameobject, so it would be picked up by this code. Since this code used an array to assign audio clips to references:

firingBullet = sounds [1]; 

emptyclip = sounds [0];

It was possible that if a THIRD audioclip was found, it would be assigned in place of my intended audio clips. So I changed firingBullet = sounds [1];  to firingBullet = sounds [2]; and...

It worked.

The background music was being picked up by this array, and because it was being played in place of the gunshot audioclip, it was restarting the background music audio clip every time.

The takeaway:

I changed my usual learning methods for this project from taking the majority of my education through books, online course and other rigid means, to simply developing a lot using Unity. I would strongly recommend this approach when learning a new skill, as it gives you the chance to learn at a much faster rate than traditional means. I have noticed that it's much easier to get stuck on something when you aren't sure about what you're doing, hence why I think that having at least SOME form of book or online course at your disposal (to solidify your fundamentals - your unknown unknowns) is still essential. I didn't do this when I started, but I really should have. I'm still tinkering with my learning process, and will be trying to learn Scala once the initial release of my Augmented Reality Unity game is released. We'll see how learning a new language/paradigm stacks up against "learning by doing" and if/what issues come about from this method of learning.