NullReferenceException: Object reference not set to an instance of an object ; Please help me

I’m a beginner in Unity and I’m creating an enemy for my 2D game. However, when I launch the game Unity tell me: NullReferenceException: Object reference not set to an instance of an object.
I don’t understand why Unity is putting this on me! However, it must be taken into account that the script is a Behaviour.


Here is my script. There is apparently an error line 28. Please tell me what to write to fix the problem!
Thank you and have a good day !

And my enemy walks but he doesn’t move. I don’t understand ! Please help me too !


Here is my script !
Help me :wink:

It’s impossible to help you directly if you never post your stack trace.
I have a resource on NullReferenceExceptions that you can go through to understand them and hopefully fix your issue.

I noticed you never told the game what rb was.

Try changing Rigidbody2D rb; to Rigidbody2D rb = GetComponent<Rigidbody2D>();