how to I set a object reference?

Hi I just start to learn Unity for like half hour(maybe?)
and Im learning the basic of the basic the Roll a Ball

here is my code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {

    private Rigidbody rb;

    void start ()
    {
        rb = GetComponent<Rigidbody> ();
    }

    void FixedUpdate ()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

        rb.AddForce (movement);
 
    }

}

I check everything is spelled right and pass debug
but when i try to run it in Unity,I keep getting the error message:
“NullReferenceException: Object reference not set to an instance of an object
PlayerController.FixedUpdate () (at Assets/Scripts/PlayerController.cs:21)”

I try to google it but to be honest the answer I found was too hard to understand for me atm
so can some nice people help me with this?

If im reading it right (Which I might not be because you didnt use CODE TAGS , grrr) it appears your Rigidbody is null. Make sure it has a Rigidbody component attached.

sorry about that :stuck_out_tongue:
but I did attach Rigidbody to the ball aka player as a component
also when I click the error message it high light the ground does it mean anything?

Make sure you didnt put this script on something that shouldnt have it. You can right click your script in the project folder and click Find References in Scene. This highlights everything with the script attached, make sure this script is only on the player and nothing else.

it seen the script is only attach to the ground but not the ball, but I follow the tutorial step by step and I have really no idea what went wrong.
does line 9 to 11 means to let the script get the “rigidbody” from the ball at the beginning of the game?
sorry i am really confused

If you have a script attached to an object you use GetComponent to access various “parts” of that object. In this case you are accessing the rigid body and are assigning it to a variable called rb. This means you can manipulate the rigid body component and refer to it as rb for short.

Get component can also be used to access components on objects other than that on which your script is attached, but I’m sure your tutorials will get to that at some point.

yes, I know this should happen, but I think my script is unable to get the rigidbody form the ball for some reasons, and I only have “1” rigidbody in the whole project, so i am sure that my script was not confuse which component to get

You have to capitalize the “S” in “Start()”.

1 Like

tried nope:(

Oh, the script should be on the ball, not on the ground. I missed where you said you put the script on the ground. But you do also need to capitalize Start(), otherwise it won’t run that code when it starts.

I did change s to S
and my problem is that i do not know how to move the script from ground to ball since I did not put anything about ground in the script, the script is only for rigidbody and i dont know why is attach to ground

You click and drag the script and drop it on the ball. Or click the ball and then press the “Add Component” button in the inspector and choose the script. I recommend watching some of the tutorials: Learn

thank you for answering my question, i did put my script on my ground not on ball ;P, but after i change it the same error still show up, but how can the rigidbody be null if I set it to rb in the script?

ok i solve it, so there is 2 rigidbody show up when i type it and I just got the wrong one, not sure why and not rush to find out why, anyway thx for everyone helped.