error in code

hi guys i am getting a error and idk why this is the code:

public class Pajaro : MonoBehaviour {

// variables
bool muerto;
Rigidbody2D pajaro;
public float FuerzaY;

// obtencion del rigibody para movimiento
void awake () {
pajaro = GetComponent();
}

// Movimiento si esta vivio/muerto

void Update () {

if (muerto == false)
{
if (Input.GetMouseButtonDown(0))
{
pajaro.velocity = Vector2.zero;
pajaro.AddForce(new Vector2(0, FuerzaY));
}

}
}
}

the code is on a gameobject called “Pajaro” and the error is this

NullReferenceException: Object reference not set to an instance of an object
Pajaro.Update () (at Assets/Script/Pajaro.cs:27)

if u push the error go to this line
pajaro.velocity = Vector2.zero;
.-.

void awake()

should be void Awake()

1 Like

Also, next time use code tags. It makes it much easier for us to read the code, see line numbers, and find your problem.

Please use code tags when posting code.

ty idk why the visual studio did not work on that line (saying that is wrong)

kk srry i didnt know :slight_smile:

Because that line in itself is valid code - it’s just that awake() is not going to get called by Unity, as Awake() will be. So pajaro was uninitialized when it got to Update(), hence the null reference.

1 Like

It did not say anything was wrong on that line because there technically is nothing wrong with it. All you did was create a perfectly fine method. Problem is Unity doesn’t call the awake() method, it calls the Awake() method, So the error doesn’t show up until you try to reference the Rigidbody that never got called in awake().

Not sure if I made that more confusing or not.

Edit: StarManta beat me by a few seconds.