Let the AI write the code

AIs have been all the rage for a while now. And rightfully so. Language AIs like ChatGPT are powerful tools and they are here to stay.

Disclosure: This text may contain affiliate links, which means we may receive a commission if you click a link and purchase something that we have recommended. While clicking these links won't cost you any money, they will help fund this project! The links are created by Unity and Partnerize (Unity's affiliate partner).

The advantages of AI-generated code in game development

Bootstrapping and Prototyping

AI-generated code can automate repetitive coding tasks. Especially the boring bootstrap code that many of us now generate with snippets, templates, macros, ... . Why not ask the AI to create a quick hello world for us. Even if the code does not work right out of the box, it usually is a fine starting point. Personally, I am looking forward to many hours saved :-)

Optimization and Bugfixing

Like any machine, AIs have the advantage that they do not mind performing repetitive tasks which require the utmost amount of pedantry (aka programming). They simply don't make silly mistakes like missing a semicolon. Once they have integrated with most modern compilers I would expect that they will never again produce broken code. It may not quite do what you want, but at least it will compile.

Consistency

No more bickering about coding styles. The AI will do it however you tell it to. It simply follows the rules and does not forget to apply them.

Translation

Code is logic and computers (and thus AIs) are good with logic. Therefore it's not a big leap to predict that soon it won't matter much in which programming language you are proficient. If the code is needed in another language we will simply ask the AI to translate it for our colleagues.

Disadvantages

Lost in translation

These AIs are still in their infancy and sometimes it is hard to make them understand what you want. Especially if the tasks are complex or require a bit of reasoning. Then it is still much faster to simply whip up the code yourself. However, once you have it, it surely does not hurt to ask the AI for improvements.

If memory serves well

Future programming will be more like searching the web nowadays. Instead of doing the work from the ground up, we will be more concerned with filtering the garbage from the rest. In this case, finding the working code that does exactly what we want.

Though herein lies a problem. While playing with these AIs I noticed that quite often I had to already know the answer to get the right result. In short, they served well as a reminder to fill in the gaps of how exactly the code was spelled, but they could not extrapolate. They had no idea what my goal was.

Taking leaps of imagination is still a problem. Creativity especially. They are trained on existing data. They can create combinations that did not exist before. Just like randomizing each color in an image can, at one point, result in a Mona Lisa. AIs will get there much quicker than a random approach thanks to their training data but once you are outside the known data, even the best AIs generate a lot of garbage. Weeding through that will become a problem once we have pushed them to the limit of what can be extracted and recombined from existing data (which still is a lot).

Money makes the world go round

One thing I like is the premium (or open-source) model of current AIs. That way maybe we can avoid diluting results with advertisements. A world where SEO is not a thing anymore would be great.

For me, it's easy to say that paying for AIs is a good thing. I can afford $1 per 1000 words. Others might struggle and we will have to find ways to ensure everyone can access this technology.

How to use AIs like ChatGPT in Unity

TLDR: Get your ChatGPT API key, buy SPARK AI and start exploring.

There are many packages offering AI support in the Unity Asset Store. We have purchased what seem to be the three most popular ones and put them to the test. We used Unity 2021 and Unity 2022 since at the time of writing those were the most used LTS versions with about 80% of all users in total [1]

It needs to be said that all the solutions in the store are actually just API wrappers for the real AI service. So they mainly differ in how comfortable they are and what boundaries they set for the prompts.

Preparations

They all use the ChatGPT API to generate their scripts and therefore none of these work offline. You will need to register an account with OpenAI and get an API key.

Here is a step-by-step guide:

  1. Create an account with OpenAI.
    Here is the link to the OpenAI registration form. The account is free but you will need an email address and a phone number for verification.
    If you want to use the AI a lot you will have to purchase additional processing power (called tokens). Four tokens are about three words and 1000 tokens cost about $0.1. This means that for $1 you will get about 7500 words.
  2. Request an API key from the API Keys page.
    At the time of writing OpenAI offers about $5 in free API calls (about 37.000 words). This may change in the future but for evaluating the product it is sufficient. You can check how much you have used on the Account Usage page.

AI Toolbox (formerly "ChatGPT Script Generator")

Our first candidate is the "AI Toolbox", formerly known as "ChatGPT Script Generator" (closed-source).

First, you will have to add your API key. You can find the input field in the project settings:

A button for testing the key would have been nice.

A nice addition is the "Generate Component" button (though you can disable it if needed).

Simple query test

In their demo video, they used this prompt to create a controller for an airplane.

"Add 3D airplane controller that accelerates automatically up to max speed. Player controls pitch and yaw."

This seems like a good use case, so let's give it a try. Here is our very simple demo scene:

Once we have hit the "Generate Component" button we are greeted with a dialog to enter our request.

Result:

The resulting script took about a minute and did cost about $ 0.001 (~10 tokens).

It does work if you chose the right values. By default, the plane will never decelerate since the acceleration is always applied and the deceleration speed is lower than the acceleration. However, that's not part of this test since what is generated is mostly relying on ChatGPT and not the tool itself.

The query is stored in the history. Though there is no real iteration.

Querying for "Make it turn slower" did not yield an altered plane script. Instead, it returned a new script that had no connection to the plane.

Complex query test

With this query, we will test if the AI actually knows a bit about Unity.

"Add a controller that changes the mouse sensitivity of the scroll wheel using the old input system."

Sadly this resulted in a broken script since it uses the new input system instead of the old one.

Verdict

The "ChatGPT Script Generator" asset gives you a nice quick way of trying out ChatGPT. Sadly it's closed-source and it does not support iterating on existing scripts which makes using it a bit tedious.

Positive:

Neutral:

Negative:

GPT AI Integration

Our second candidate is the "GPT AI Integration" (open-source).

Right after importing into Unity 2022, it shows an auto-upgrade dialog for deprecated scripts. Nothing too serious, though it signals that the author has not yet adapted it fully to 2022.

Like all other assets, you must add your API key first. But how? There seem to be no preferences and clicking ChatGPT in the menu throws some errors.

The answer can be found in the manual:

It seems we have to enter the API key in some ScriptableObject file which can be found here:

Okay and that file points to some other file on my hard drive.

I did not feel like creating that file so I chose the other "string" option.

I don't think "organization" is required, so I just left it empty. Sadly the inputs have no tooltips.

I have also noticed that if the Auth Type is set to "String" then the API key will be stored as plain text in the Scriptable Object. Users should be warned about that. I am pretty sure some will commit their API key accidentally to their VCS.

Simple query test

Okay, now let's try to generate a controller for our airplane demo.

"Create a 3D airplane controller that accelerates automatically up to max speed. Player controls pitch and yaw."

The result is the full reply from ChartGPT. In order to save the script we have to delete the plain text and hit the "Save as" button.

The script did not compile right away.

It turns out ChatGPT has to be asked to import the proper namespaces.

"Create a 3D airplane controller that accelerates automatically up to max speed. Player controls pitch and yaw. Import all required namespaces. Add RequireComponent attributes for any component used in the script."

Unfortunately, this error kept popping up afterward. Not even removing and reinstalling the asset could fix it. A look into the code reveals that even the code throwing the error is faulty (using an index out of range for reporting the element of an array). It seems someone has tried to write his/her own JSON parser and did not quite succeed. Only after fixing some scripts by hand I got it to work again.

Result:

The final script worked and did make the plane fly. So, if it works then it's a valuable tool.

Complex query test

With this query, we tested if the AI knows a bit about Unity.

"Add a controller that changes the mouse sensitivity of the scroll wheel using the old input system."

Just like the other assets this resulted in a broken script since it uses the new input system instead of the old one. Seems like a ChatGPT error.

Verdict

Sadly the script errors in the "GPT AI Integration" asset itself and the not-so-great UX make it a hard-to-recommend tool, despite the many features it has. But it definitely is an asset that should be revisited once it has matured a bit. If you get it now and the author keeps improving it then I think you won't regret it. And it's open-source, which is something that can not be said about the other tested assets.

Positive:

Neutral:

Negative:

Spark AI

The third tool tested was "SPARK AI" (closed-source).

After importing into Unity 2022 it shows a friendly welcome dialog with a link to the settings to enter the API key.

If no API key has been entered then the log is flooded with errors, like this:

Once the API key is set it vanishes, all good.

The Spark AI main window has grouped every feature into a tab. That's neat.

It also offers some handy script creation and script iteration buttons for your components.

Simple query test

As with the other assets we have given it this simple test query:

"Create a 3D airplane controller that accelerates automatically up to max speed. Player controls pitch and yaw."

Result:

The result is a nice dialog to save the script.

Unfortunately, the creators of the tool have missed that the default folder for saving (/SparkScripts) may not exist.

Not a big problem. We can specify an existing folder in the settings. Though it would be nice if the tool just generates the missing folder.

Iterating the script

Spark AI also offers an iteration dialog for your scripts. Let's try adding a "jump" mechanic to our plane.

The "Create Script with AI" dialog shows the full ChatGPT response. We have to copy the script part and save it ourselves. Not ideal.

Like most of the other tools, it does not add [RequireComponent] attributes for the components it adds.

Luckily there is an "Ask for modifications" button. Let's try that.

Oh, nice. It did what we asked. And now there is a new "revert" button. Awesome!

Complex query test

With this query, we tested if the AI knows a bit about Unity.

"Add a controller that changes the mouse sensitivity of the scroll wheel using the old input system."

Just like the other assets this resulted in a broken script since it uses the new input system instead of the old one. Seems like a ChatGPT error. After three or four iterations it suddenly started offering solutions with the old input system. Very interesting. The AI is learning.

Verdict (the winner)

In my opinion "SPARK AI" is the best and most complete AI toolkit currently available for Unity. There are some areas to improve but overall it has good UX and just works. Being close-sourced is a bit of a bummer though.

Positive:

Neutral:

Negative:

What does the AI think about all of this

I asked Chat GPT to list ten advantages of AI-assisted programming. I was surprised. The list matches quite well with what I thought.

Not all of them make sense to me though. But hey, at least this tells me that this AI is not that intelligent yet. We may still have some time left till the machines take over the world.

Thanks for point number 9 ChatGPT. Now I feel really appreciated as a programmer (being just a cost factor, ya know). Maybe I should scratch that one before handing this report over to my boss.

When asked to predict the future we tend to just linearly interpolate the current state. In truth, change is often exponential. This can be an advantage (technology) but also a disadvantage (climate change). The digital revolution has been by far the fastest change we have ever seen and chances are that this AI thing will accelerate it even more. Turning a blind eye is not an option but we should consider what it will do to us.

Asset Recommendations

I hope you liked the article. Here are some asset recommendations.

Disclosure: This text may contain affiliate links, which means we may receive a commission if you click a link and purchase something that we have recommended. While clicking these links won't cost you any money, they will help fund this project! The links are created by Unity and Partnerize (Unity's affiliate partner).

Sources

  1. Unity Usage Reports (2023-04-10): https://forum.unity.com/threads/editor-version-usage-information-updated-march-16-2023.485317/