How to DEBUG these 14 common ERRORS in Unity 3D | Beginner Level

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to today's video in today's video i just want to go through some common console errors that we will get and some tips on how to debug our code obviously i do a lot of tutorials on the channel and a lot of people will comment under them saying oh this isn't working that's not working i just want to kind of try and give you some skills on how to debug some of the console stuff because obviously i'm i'm more than happy to help and people have been posted in the comments and on discord uh but i can't always get to them you know straight away and then you're just sat around not being able to follow the tutorials so hopefully you'll learn something in this video about how to debug your own code and solve some of the really common issues that i've been seeing in the comments um so let's just get straight into it so i've got some scripts here i've got three scripts and there's all sorts of stuff wrong with them obviously this is very contrived i've set it up so there's a lot of different things that are broken and we're going to go through and fix them uh sort of one by one so if i go over to unity so this is the first thing so let's just start with the very basics so this is the console and these are our errors and if you actually click them and read them these ones are quite simple errors it'll tell you exactly what's wrong and it'll give you um the folder and the script that it's wrong in it'll give you the line where the error is as well it'll tell you what you know what it thinks is going wrong and then you could either go to that script and navigate there or if you just double click the error it will take you directly to the script so you can see here if we look back at the console it's saying that there's a semicolon expected on line 48 in our issues.cs class we're at line 48 and it wants a semicolon so we can do semicolon so and then we can save we'll go back over to unity and that's got rid of that and then you'll see you know again same script line 53 there is a closed curly brace expected so again we'll double click we'll go back and you can see this line here um it's kind of it doesn't hit another um curly brace we've got a curly brace then the line kind of tells us that there needs to be a curly brace around here somewhere so let's put that in oh we'll do a curly brace hit save let's go back so this will recompile and it'll throw up all our other errors so this is the next issue so the type or namespace name issues could not be found are you missing a using directive or a assembly reference again this is in our issues tester script on line eight so let's double click it and we'll go through to that so you can see here that we've got public issues and issues is read but we know that this class exists because it's called issues we've got it here we can also see that it's in the namespace issue namespace so we go back over to issues tester and with rider which is the ide that i'm using and i can press alt enter i believe with visual studio it's control and the period key and that will generally tell you what the issue is so if i press alt and enter here we can import missing references in file index or import type issue namespace issues which is what we want we know this is a namespace issue because the error in the console kind of hint to that are you missing a using directive and it says the type or namespace name issues couldn't be found so if we um press alt enter we are now using issue name space which is our name space here and that's that error solved and you can see that that's gone from red to this nice purple color um and again on visual studio you can press ctrl and period and that'll bring you up to the same uh window sort of something similar to this and it'll usually tell you to import the type and it'll just click that and it'll do it for you so that's that error fixed let's jump back over to unity okay so now we can see we've got a load more issues here as well we can kind of ignore these yellow ones these are warnings it's just telling us that we're never actually using the variable i they're good to keep an eye out but they won't stop your code from working unless you did mean to use it and you've just forgot to use the variable somewhere but it won't stop the code from it won't stop the game from playing necessarily you can see we've got two of these here um but so we go back over to assets and issues tester line 13 we've got um my method is inaccessible due to its protection level and again we know the class we know the line and we can double click it to take us there so we see here we've got issue script my method and we've got a red squiggle which says cannot access private method uh my method here if we go over to our issue script and look at that method i've set it to private which means that we can't get access to it from our other script it's a simple fix we'll just change that to public back opportunity let's go over to that one so now we've got an object reference is required for the non-static field method or property issues my public int and again this is line 15 of the issue tester script let's double click to take us there so here it looks like we're trying to access the issues class and we're trying to get access to a public ins you'd think that it would work on the sort of surface of it but we are accessing the um class type not the actual object reference so we need to know exactly which script we want to be calling this my public in from and again if i press alt enter or you could press control and period it would usually give you some sort of tips on how you can fix it in this case we know that it wants an object reference not a class type reference so instead of referencing the class type we need to reference the object so we can do issue script dot my public int so that's that problem fixed now we've got the reverse of that so remember issues dot my static ink can't be accessed with an instance reference qualify it with a type name instead so that's the reverse of that so on our issues class we've got a static in which can be accessed from kind of anywhere and we don't need a sort of object reference we just need to reference the actual type so we can use issues instead of issue script and then we can access our static in so that's that one fixed so the name my bool does not exist in the current context so let's double click that that's on line 42. so we've got debug.log mybull if i alt and enter um it's saying that there's not a local variable called mybull and i'm sure i've set that up so let's go up i've got private bull oh and i've misspelled it it's my cool instead of my bull now this is a very common issue miss spelling names because there's not a you know a spell in kind of spell checker it's not always a built-in sort of spell checker um that could be you know a really simple you know that's one of the most common kind of things i would come across um you know sometimes you'll see people's with their private syntax they'll have an underscore and then you'll have missed it here and you'll be like no it's definitely something called my bull it's because there's a character missing usually or it's misspelt wrong or there's just not a you know there's a typo um you've missed off the kind of semicolon which was the first area we looked at um a lot of the times it's very simple stuff like that so in this case we'd said my cool would instead of my ball but we were accessing it with the correctly spelt version down here and that was causing an error so always check your spellings so let's jump back over to unity this one issues line 49 issues parent that my private flow is inaccessible due to its protection level so we've come across this issue um a little bit earlier so let's see what's different about this version so let's double click that so here we've got my private float is equal to five f and we've got my protected flow is equal to 10f so if i go up here and we don't actually have my protected flow of my private float listed here but we can see the issues is inheriting from a base class called issues parent so if we jump over to the issues parent class i'll go through here you can see that we've got a private flow my private flow and we've also got protected flow my protected flow so this is a really common issue that you might come across again especially so if you've got scripts inheriting from a base class you can access the sort of stuff from that base class from the child of that class but you can't do it if it's marked it private so private is for this class specifically protected is for this class and anything that derives from this class and then public would be from any class so make sure that you are using protected variables if you want to access them sort of like further down the child sort of the child line you need to make sure you're only using the protected versions and that's the same with methods as well so you can see we've got a private void my private method and a protected void my protected method so within this method here if i do my private method and i know this is a method on my um on the parent and you see that it's come up as green so it's definitely a um so that's definitely a method and it's you know we've spelt it right but we're getting the squiggle and again that's because uh it's inaccessible due to its protection level we need to use protected instead of private so that's just something to keep in mind as well so let's go back over to unity okay so that's all of um you know the errors gone we could now hit play and it would run uh let's get rid of these kind of warnings have a nice clear log as well so that's because we're not using that so let's debug.log i so we're now using that one and then let's just uh delete these we don't need them okay so that's a nice clean log no issues so let's hit play and see if the game is actually working kind of at run time so hit play and immediately in the console here uh we have an issue so object reference not set to an instance of an object now on the sort of face a bit it doesn't give you this sort of script and script line but if you actually look closely it does it says assets issues dot cs line 26 and it even tells you the kind of um method that it comes from so it's the start menu on our issues and it's specifically uh line 26 so let's double click so here we've got my in list dot count and we've got my int list up here but we've never actually initialized this list it's uh null so what we can do is we can say this is equal to a new list of type int or we could do it in the actual start method as well so we can do start my list in is equal to a new list of type int so let's see if we've fixed that and we'll hit play okay so we've got another issue here so let's take a look at this issue so unassigned reference exception the variable game object reference of issues has not been assigned you probably need to assign the game objects reference viable variable of the issues script in the inspector so it's specifically telling us what the problem is and telling us kind of what we need to do to maybe fix it now we actually click the error you can see down here um it says the same thing but it will tell you the script the method and the line that it's coming from it doesn't say that up here but it will say it when you investigate the kind of log a bit further so again double click we can see that it's this line here we're trying to set the game object references position just to a new vector but that's not actually working because we haven't signed assigned it in the inspector so let's make a new game object and we can assign that in the inspector so that problem shouldn't be a problem anymore i'm just gonna put this down here so it doesn't keep taking away from the console so we'll hit play okay so now we've nearly fixed this issue so okay so we've got a new error now so now we've got an index out of range exception index was outside the bounds of the array and again it's pointing us to line 36 of the issues script so let's double click so here we are debug.logging and we're logging a and we're connect so here we're doing debugger.log and we're trying to access index five of the my interarray so let's have a look at our my interarray so that's here got my inter a and we're setting it equal to a new in array with a size of five so you'd think that maybe that should work but it's not and that's because you probably know this by now but arrays are zero based so when we create a new in array here we're setting it the size we're setting it to a size of five then to access that index in our array it's actually going to be from zero to four because they're zero based so we have to knock one off this when you get those index out of range it's because you've gone kind of outside of the arrays range so to illustrate that we can do debug.log my int array.count dot length sorry we will access number four and then we'll try and get number five so we'll come out of here so our ent array has a size of five index number four has a zero in it and that's just because that's a default in number and we didn't actually set index four to anything then we get our index was outside the bounds of the array so that's just something to be aware of and you may know that your count is um you may know that the array's length is five but it with arrays being uh zero based we have to either do um a length minus one to get the last slot in the array or just make sure you know if we're in a loop that we're doing it to the length so what you do need to do is instead of putting in five directly you could do the length minus one and that will offset it back to sort of being zero based and better yet if you want to you can um do an index from end expression so let's actually set this to um instead of just giving it a size of five let's actually set those values so we'll do one two three four five so here the values of our enter rate are one two three four five there's one two three four five elements in the array if we debug dot log element four we're gonna get zero one two three four so we'll print out five and then hopefully this debug.log will print out five as well there we go five five five um so they are our debug.logs working and if you don't want to do my in array.length minus one you can use the index from end expression so we can use the um like up bracket with one and that should do the same thing so we've got five five five so it was a bit long we focused on that for a bit longer but that's a really common issue that quite a lot of people have um by going outside the bounds of arrays especially with the inventory system tutorial i've been doing that's kind of list an array sort of based so this comes up quite a lot um but that all seems to be working so let's see what this next issue is object reference not set to an instance of an object and this is in our start method as well so just like our um in list the dictionary hasn't been initialized it's not set to anything so we could do my dictionary is equal to a new dictionary of type in and float okay so here we are initializing our dictionary so that's fixed that issue so now we're going to go into a loop and we're going to add to our dictionary um in i which is going to be this index and we're going to just make a random flow in that so that's going to go into our dictionary and i've done this based on my inter a dot length which we know is five so it's going to go from and we're doing i is less than five so we're going to go from zero to four we're to add them to the dictionary plus we're going to so we're going to go through a loop which will be 0 to 4 and then we're going to add to the dictionary the index plus our random float and then we're just going to debug log the flow actually i'm going to get rid of this because then we're going to go into a loop on our dictionary we're going to go through every key value pair i'm going to print back out the key and the value i'm going to do our other stuff and just to keep the log clear let's get rid of these we don't need to do that anymore because we've already fixed that issue oh no we'll keep that unless why not and then we're going to go into another loop and then trying to add to our dictionary again and we're going to do the same thing so while 0 is less than 5 we'll add that index into our dictionary and then we're going to print them back out so let's see if that will work let's jump back over to unity so we're going to go through this is our dictionary we see that's working fine but then we hit this one so an item with the same key has already been added key zero so it's breaking on the first kind of loop around and if we read through what's going on here we can see again that's line 42 we're trying to add to our dictionary but we can already see that the dictionary contains a key and we can have we can have duplicate values but the key has to be different every time we can't have the same key so what we could do is we could increase this to say less than 10 and then we could actually put if my dictionary dot contains key i and we'll do if it doesn't contain e i if it doesn't contain key i which is our index then let's add it so this should go from one to zero to four in this version and it'll print them out and then we're going to go through it again and we'll it won't give us an error and because we're not going to be duplicating trying to add anything in because we're only doing it if it doesn't already contain the key so let's jump back over hit play there you go we have no more errors okay so for the last kind of tip let's hit play and see what the error is here you can see we've done a lot of stuff but we've got this issue here so let's click this so we've got that same issue that we've had previously so object reference not set to an instance of an object we double click that you can see here that that's this we're trying to debug.log this empty list it hasn't been initialized so we've seen how to fix that issue by making sure that we declare our list and actually create it um so we know how to fix that but sometimes you might be confused as to why this is throwing up an errand like what specific chain of events is causing this method to be called now you see in um some ides you can see which scripts are using it we can click one usage here and it'll show us where exactly we are using it but if you click the script you can see here that going backwards in time it shows you the chain of events so the issue here we've got it's on it's in this test method on our test class in the issue namespace and it's line 12 so that's this here we know like i said we know how to fix that we can initialize it and by setting it equal to an instance of a new list and test method is being called by my protected method which in turn is being called by my method which in turn is being called in the start function so we can actually track back the chain of events so we can see here that in our start on our issues tester we're calling my method and then from my method we're calling my protected method then from my protected method we are making a new instance of our class and then trying to call test method so that isn't necessarily about how to fix this issue because again we have figured out how to do that we'll just fix it like this so that will have fixed that issue but that showed us how we can follow a chain of events back from within an error log you can see we no longer get that error so i hope this video was useful i tried to go through them quite quickly to cover them off they are very common uh issues that people have hopefully this all made sense to everyone and if it didn't i would suggest just going back through it again and but the main takeaway from this video is just read the error logs double click them choose a bit of logic and you should be able to figure out kind of what exactly is going on and what the problem is just based on the issue there alone because being able to problem solve yourself and not relying on the comments or other people to try and help you and eventually by learning all this you'll move away from following tutorials and you'll start to be able to kind of research things yourself by reading through the say the unity documentation yeah i hope this video was useful as always this information was not free if you're not subscribed to the channel you need to hit that subscribe button as payment i'm sorry and if you are subscribed your payment can be a little cheeky like by clicking the like button but yeah thanks for watching and i'll see you in the next one bye i'd just like to take a minute to thank my patreons in the 10 000 expert here we have p-o-t-e potepote i don't know how you say that and in the 4000 xp here you can see all of those lovely supporters on screen now thanks everyone for your support
Info
Channel: Dan Pos
Views: 1,293
Rating: undefined out of 5
Keywords: unity tutorial, game dev, indie dev, game design, unity 3d, made with unity
Id: zfHfj6GGWo8
Channel Id: undefined
Length: 22min 3sec (1323 seconds)
Published: Thu May 12 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.