I dont understand why this reference null exception is triggering


At this point in the tutorial i am unsure what caused this error, as i believe i followed everything correctly.

Either bulletProjectile or e.TargetUnit is null. Make sure that the prefab has the BulletProjectile script (on the root game object), and that you are assigning the target unit in the OnShootEventArgs

In the photo ive attached you can see the Unit prefab has the BulletProjectile(Tranform) attached to it via a serializedfield. Is this what you mean by making sure it has it on the gameobject?

I will look into the second part of your suggestion after work.

Thank you for your help and sorry for the late reply. It is 8:30 AM here.

No. That field should hold the prefab, but the actual prefab is the game object that is the projectile. It should have the BulletProjectile component on the root because you are using GetComponent to get it. If it is not there, it will return null - which may be the source of the error

Ok, I understand your suggestion fully now. I will check when I get home later, and will update you with my results.

Thank you!

The BulletProjectile prefab was missing the script, as you mentioned.

Thanks for your help, what a silly oversight on my end.

I think it happened when my Unity crashed but my VSCode didnt so my scripts were there, I just didnt re add the script after the crash.

Once again, thank you.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms