Blogs

Unity Scripting

I have been studying, experimenting and generally learning Unity for a few years now. I previously worked with Playcanvas, which was an excellent platform too. The reason for switching was to stretch my skills and try something a bit different with a different language, namely C#.

Over time, I learned so much, but each time I think I have gained sufficient skills, I move in to a new sphere of study. It is very rewarding, but equally frustrating. There really is so much to learn and above all, retain, that you begin to doubt you will ever achieve anything. You often forget how much you have learned and tend to ponder on all you have yet to master. In short, you feel useless most of the time and overwhelmed on a regular basis. But don’t despair. You are getting there and believe it or not, you are probably way ahead of most people trying the same thing and not as far short of the better people as you may think. But you must keep at it, always take a little time away if you start to feel stale and disheartened, hours, days, weeks or even months if necessary. But always come back, the achievers always do.

The main thing I have learned about scripting and development this year (2022), is that you can learn syntax, but what you need to master, is the ability to read code and make an image in your mind of the script and what it is doing. Build up an ability to read code and paint a picture of the paths each variable, function and method is taking. This is the way to help you build your own solutions. Draw a map of how to achieve something or solve a problem, either in your mind, or on paper. Imagine it as a map on how to get somewhere, with all the twists and turns, stops, dead ends to avoid, how to negotiate obstacles etc. Once you can visualise the journey, you can begin to slowly turn the solution into useful code. Just trying to code the solution immediately can be fraught with frustrating difficulties, as you hit problems in your logic, which are not apparent when you are also trying to remember what the syntax is doing at the same time. You need to code something already figured out, at least a rough mapped solution. You can start with headings of all the functions required in plain English and descriptive titles. Add a few lines to each function to describe what needs to be done and how.

I won’t spend any more time at present on the above, as I want to keep this a quick and easily digestible read. Suffice to say. An example in very simple form would be to program making a cup of tea. How do you code that for a computer to understand? As simple as possible is the answer and no solution is perfect or the only one you can produce, but you have to produce something.

Variables
Electicity
Kettle
Teapot
Cup
TeaBags
Milk
Spoon
Tap
Water

Functions
CleanAllItems
FillKettleWithWater
AddTeabagToCup
BoilKettle
IsKettleBoiled?
PourBoilingWaterInCup
StirTeabagAroundCup
AddMilk
RemoveTeaBag

I am going to stop there to keep it simple. You can see how just making a cup of tea involves many functions, or at least potentially, if you let it and the functions above are not exhaustive and may need a couple more. You can begin to develop your solution by asking what elements require a boolean true or false trigger? IsKettleBoiled? This is the obvious boolean. But you can also ask if cup does have a teabag in it yet? Does kettle contain water before turning it on? Are all items clean? Is the water hot? Prior to this, a perfect example of why this planning is required is setting your variables. Have you remembered everything? When putting together the variables, I forgot to put in Milk and Spoon at first and typed them in subsequently after reading through to see what I may have missed. If I had coded immediately and forgot those variables, my solution when run would have produced tea without milk and nothing to stir it with. Yes, I could have added this in at coding time, but when producing a more complicated solution, this would have meant sifting through complex syntax and statements, all taking up precious time. Time is never a good thing to waste when coding. Coding is a massive waste of time, it takes up many, many hours and time can easily run away with you, 7pm can turn in to 1am very quickly when you are very in to what you are doing.

To Conclude, you can code for years, know lots of syntax, understand and pass many language examinations, describe yourself as proficient at C#, Javascript, Python or any other language. But can you actually code something worthwhile that does something that people want. Quite possibly, but you can also fall in to the chasm of Programmers who never got past this obstacle. Take someone who says ‘I know how to build many types of wall’. How many walls have you built and are able to show anyone? Errrrrr, I have half completed loads of walls, never quite finished them, as each one got a bit complex and gave up and started a new one. If you are that person, it is time to stick with that first wall and get it completed. Delve in to what you don’t know, the part you stopped at to start a new one instead, don’t be scared to keep getting it wrong, but make sure you finish it, no matter how tedious it seems to feel. You’ll be glad you did. Then you can begin to build a better, more complex wall and eventually, rather than just a garden wall, maybe one eventually incorporated in to a building, or whole house. Mark Robbins 🙂

Leave a Reply

Your email address will not be published.