Can ChatGPT Generate Unity Code?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the robot Uprising has begun so I figured I would see if an AI can create good tools for Unity and if we as developers have a chance to survive so I jumped on chat GPT and post some ever increasing challenges so watch to the end to find out if developers are really out of a job if you came for the controversy this won't be the video for you this is more of an unbiased view of me scraping the surface of chat gpt's Unity code generation and my prompt game so without further Ado let's Jump Right In with something basic and we'll go from there so first prompt write a Unity editor script that selects all game objects in the scene okay so it's deriving from an editor class it's not absolutely necessary when we're just creating there we go a menu item it's given us a good name for our menu item and it's also named the class and the actual function quite well it's also giving us a shortcut now we didn't ask for that but that's fine it's actually a teaching moment and also we want to check that that shortcut doesn't conflict with other shortcuts that we create now looking at the code it's finding all objects of type game object and it's placing those in the selection which is perfect that's what we wanted it to do now the nice thing is it also gives us some comments it tells us where it's going to put it so in our toolbar it'll be added edit and it'll be called select all game objects which is a good name for this it also tells us what the script does now it'd be very nice to actually use that details and put that in a comment above the function there but you know we can do that ourselves and that it also tells us that this is an editor script and it's not for actually running in the game okay so let's copy our code and it's great they give you a little quick Copy button there we'll jump into Unity where I have the stinty construction set from the asset store and I'll leave a link in the description if you want to grab it yourself because you like what you see here and I'm going to create a new C subscript and I'm going to call this select or game objects because that's what the AI called it and let's open that into Visual Studio Okay so we've got Visual Studio here and I'm just going to take everything and I'm just going to paste my script over it and I will save and let's go back into Unity now okay so we're back in unity and we're looking for our prompt and we know it's under edit which is quite handy so we go to edit and we've got everything down here and right down the bottom you can even see it's put in the shortcut there Ctrl alt a and if I press that then great there we have it we've selected all the objects in our scene that's kind of perfect okay so back in chat GDP let's have a look what we can go further with so right a Unity editor script that The Lex all game objects with the layer character let's see what happens there okay so here we go so same thing again brilliant good title adds it in uses the same shortcut which will be a problem but we can just delete that off we don't need to use that and there we go look it's getting a game object it's putting a link statement and it's putting it back into the selection so it's giving us all the relevant details as well which is really handy obviously and it's going to be under the edit menu which is funky so let's do it again so let's copy that because we know that's going to be what our script name is so create C sharp script pop that in there that's a long old name let's open that up in visual studio now we'll put back in copy our code go back into Visual Studio and we'll paste our code in and let's get rid of this shortcut because we know that's going to be there ah you see here's our first error it's put in the where here from the link library but it isn't actually brought it in so we can control Dot and using system link so that will fix that error there so let's save jump back into Unity okay so now we're in unity we actually have some characters popped around and as you can see here's one of the characters and we've got the layer set at the top there that's pretty special a little bit more there we go character so let's try it out so editor select game objects with character layer and we've got our selection here's the character objects all our characters were selected let's just do that again so we can see it select all of our characters these are the only ones with the actual character on them brilliant now the next prompt I tried was write a Unity editor script that turns an object by 90 degrees in the y-axis this time it actually created it as a mono Behavior which was odd as soon as I put the menu item in and stood on all the static Etc looking back at this I realized that actually in the prompt if you use Unity editor tool it will create it as an editor rather than a monobehavior so that's just something to bear in mind but as you can see when we copy and pasted this into unity and tried it out sure enough it turned the crane that I selected 90 degrees so the next prompt was quite a tricky one should people like this video I'm making And subscribe to what Imagination YouTube channel of course being an AI it has no opinion on the matter but you all know what the right choice is to press like and make sure you subscribe to the channel so now I wanted to make the prompts a little bit harder and I wanted to take a previous video which was on the scene selection using overlays and add to it by doing that toggle that I mentioned in that video about selecting only the scenes that are in my build now the first function I actually tried about the first prompt basically all it did was give me a monobehavior that returned all the scenes in the scene manager which obviously I didn't want that's something you can actually use at runtime but the second time I actually changed my prompt to say I wanted a Unity editor function that returns and this actually gave me an interesting result it gave me a static class of c new tills and it returned what I wanted all the scenes that were enabled in my build settings so there you are there's the code that you could add to that toggle from the previous video talking of that previous video I wanted to stretch the actual program so I asked it to write a Unity editor tool that creates a toolbar overlay for selecting a scene from your project now this is exactly the video that I created last time so you can imagine I'm interested in seeing what it can actually produce now as you can see it starts into a quite extensive editor window which is great it creates an editor window it enables the menu item it gives you an on GUI that actually puts in a GUI layout toolbar that enables you to select a scene and open a scene and splits off all that functionality into different functions like get the scene names and open the scene and that's all great code now obviously it didn't do what I wanted it to do which was create a toolbar overlay but I'm guessing some of that's to do with the fact that toolbar overlays aren't really covered an awful lot and that it's a newer piece of functionality in unity and if you were to do it before toolbar overlays well this isn't a bad result on how to actually do it so just be aware of some of the results you might be getting back might not be the latest and greatest functionality that you'll want to put into your Unity projects now I wanted to highlight the next prompt because it was commonplace for what I got back from chat GPT and it shows the sort of things you have to do to actually enable this code to work in your particular build and your Unity projects so let's look at what I've done here I've done a previous video again and it's the unity decorator drawer for displaying a horizontal line in the inspector window so let's try this out we'll copy this code we'll come into Unity we'll create a new c-sharp script horizontal line Aurora we'll open that up let's paste this over and we can see that there's a problem there it's not got all the library so that seems to be quite typical of some of this stuff and you have the control dot after it to basically bring the libraries in now I know there's going to be a problem can you see already but I'll show you in a second let's go back into our prompts we'll go down here and we'll take our my script we'll come up now this of course is a script it doesn't want to go into the editor folder it actually wants to come under the scripts folder so we'll create a new folder called Scripts and we'll create a new c-sharp script under that called my script and we'll open that up in Visual Studio reload it there we go and we'll paste it in well it's got the typical thing how's it got the libraries Unity engine there we are okay now when we try to use this it can't find it and why well it's because this particular attribute is actually in here which is under the editor folder and as you know if you've been working in unity for some time that the editor folder scripts all your scripts that are under this editor folder will basically be compiled into a separate dll from the scripts in your scripts folder this is your runtime this is your editor and the editor can see the runtime but the runtime can't see the editor so how do we fix this so what we want to do is we actually want to create a class with just this so let's copy this up we'll come back into Unity we'll create a c-sharp script and we'll call this horizontal line attributes open it up in Visual Studio we go paste it in okay now we want to get rid of it in here obviously there we are so we'll get rid of that so now we have a horizontal line and also the script can now see horizontal line great stuff so if we save it and we come back into Unity now if I was to create a new game object there we are create a new game object and under this game object I'll put my script in there and you can see there's that horizontal line right there for you to use now if that didn't exist if I came back in here and delete this horizontal line just to show you it's actually there once this recompiles they have it the horizontal lines gone so it does work so in conclusion am I out of a job well no I'm safe for the time being however I do promise to add a comment on this video to a future video of when that changes and I'm pretty sure it will at some point now is chat GPT useful for developers well yes it is and let me explain why you look up how to code something all the time as a developer as you don't want to reinvent the wheel so you could think of this as another source Forge or a Unity tutorial site or wherever you get your inspiration from but like those sources don't take everything you read as golden you've got to know your stuff copying and pasting someone's code without knowing what it does or how it does it it's not going to improve you as a coder and it also lead to complications down the road when everything goes bang but for now though with all the intended sarcasm let us welcome the robot Uprising in all its AI glory and watch the next video while we wait for it actually to happen
Info
Channel: Warped Imagination
Views: 13,394
Rating: undefined out of 5
Keywords: unity, unity tips, unity beginner, unity3d, learn unity, unity tutorial, game dev, unity tutorial 2022, c# unity, made with unity, game development, game dev tutorial, game programmer, indie game dev, unity cheat, unity performance, unity debug log, unity logging, unity AI code generation, AI code generation, Chat GPT, Unity Chat GPT, unity ai, chat gpt code, chat gpt coding, midjourney, unity midjourney, open ai, unity open ai, chatgpt, ai coding, ai programming, devlog
Id: AUjw7dvVhOw
Channel Id: undefined
Length: 12min 9sec (729 seconds)
Published: Mon Dec 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.