Error - NullReferenceExecption

I have followed the code from the lecture but I am still getting a NullReferenceException at FindObjectOfType().AttackerKilled(); in the Attacker script. I have even tried copying the code directly from lecture project changes but nothing is solved. is something wrong with my code? or is it something else that I am not seeing?


script

Hi AmanKhan,

Welcome to our community! :slight_smile:

Thanks a lot for sharing so much information on the problem. That’s really helpful.

In line 20, the FindObjectOfType method looks for a LevelController object and tries to call a method of that object. Since you got a NullReferenceException, the FindObjectOfType very likely did not return a LevelController object. Methods themselves do not throw a NullReferenceException. That’s why I know that FindObjectOfType<LevelController>() very likely returned null (= no object reference).

Check all game objects in your Hierarchy. Is there a LevelController component?

Hi Nina,

Thank you, its an honour to be a part of this awesome community!

I am not sure if Unity was being buggy before, but its all working fine now. I checked again after sometime and the error was gone.

Yes, a LevelController component does exist in the Level Controller GameObject (as seen in the picture above). So, the NullReference from before still remains a mystery to me, but thankfully everything is working fine now.

SOLVED.

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

Privacy & Terms