myRigidbody.velocity causes error and my character will not move

My code is identical to Rick’s and yet I get this error message.

I tried adding a bit more code, hoping it would fix itself, but it will still refer to the same line containing .velocity.

I have tried closing Unity to see if that fixes it but no dice. Any guesses?

Your code is not identical to Rick’s. You have not assigned anything to myRigidbody. This is what Rick’s Start() method look like

void Start()
{
    myRigidbody = GetComponent<Rigidbody2D>();
}

your’s look like this

void Start()
{
    myRigidbody.GetComponent<Rigidbody2D>();
}

There will be one error message for this error at the very top, and then the error messages each frame for the one with .velocity

That is a very good catch! I would have never spotted that on my own. Got code blindness from trying to compare the two for too long.

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

Privacy & Terms