Fix Null Reference Exception in Unity

Опубликовано: 20 Март 2019
на канале: Info Gamer
28,813
344

Here I have another debugging tutorial on how to fix a NullReferenceException error in Unity. This lesson will teach you more about receiving a Null Reference Exception error. I will teach you when and how one might receive this kind of error and then I will teach you how to fix this error so you are no longer receiving a null reference exception error.

Discord:   / discord  
Website: https://infogamerhub.com
Become a Member: https://www.infogamerhub.com/membersh...
SUBSCRIBE for regular Video Game making videos:
   / @infogamer2145  

In this example, I am receiving a NullReferenceException because whenever I left click with my mouse I send out a raycast from the camera to the mouse position is world space. If there is no object for my raycast to hit then it will return a null value and save it into a variable. I then try to get the tag of that variable but because the object is null it throws the Null Reference Exception. You can fix this error by first checking to see if the return hit value is null. If it is null then you will want to do nothing but if it returns a proper value then you can access the tag.

Now you don't always want to just check to see if the value is null. Depending on the circumstance the value of what you are accessing might be required for the logic of your game. In this case, yes, you might not be receiving the NullReferenceException error anymore but your game will also not work properly.


На этой странице сайта вы можете посмотреть видео онлайн Fix Null Reference Exception in Unity длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Info Gamer 20 Март 2019, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 28,813 раз и оно понравилось 344 зрителям. Приятного просмотра!