New to Unity, Could use some advice on how to fix this error

Hey guys this is my first time posting on here and i just really need some help on this error that i cant seem to fix on this 3D FPS game I’ve been working on for my game design class. I’ve been getting 2 “NullReferenceException” errors when trying to use a script that’s supposed to activate a particle system every time i shoot the target to simulate blood. I attached a picture of the errors and a picture of the script. Anyone got any advice? oh yeah and im on version 2020.3.14f1

Code-related questions are best posted here: Unity Engine - Unity Discussions.
You may also want to read the content at https://forum.unity.com/threads/how-to-fix-a-nullreferenceException-error.1230297/ .

If an error occurs, it would be a good idea to check whether the child object of the object that was hit contains Blood.
If it is included, there may be a problem with the Play() part.

1 Like

As Kurt will tell you, the first thing to do is work out what is NULL.

In this case it looks like you probably don’t have a Blood component attached to your Enemy game object.

Hi,

Please use Scripting, or Getting Started sub forum for such questions. General sub forum is not the place for such questions.

Don’t ever use screenshots for scripts / code. Nor phone photos.
Use code tags instead.

9826275--1412667--upload_2024-5-10_10-59-24.png
Screenshots of error log in console are fine.

1 Like

The error indicates something going wrong in line 25. In that line, quite a lot could be going wrong:

hit.transform.gameObject.GetComponentInChildren<Blood>().Play();

This assumes that hit is not null, and that it has a child object that has the Blood-component. I am assuming that whatever is being hit does not have the Blood-component added to it, which causes the error. You could add a check like if(hit == null) continue; before line 25, or rewrite it a little:

if(hit == null) continue;
var blood = hit.transform.gameObject.GetComponentInChildren<Blood>();
if(blood != null) blood.Play();
1 Like