object reference not set to an instance of an but works with other script

this is probably a common problem that i will say “oh duh” to once its solved. but i keep getting a reference error when trying to access the magnitude of the rigidbody of another game object. my code for the object being referenced looks like this:
PlayerController.cs

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

	public float speed;
	public float jump;
	public Rigidbody rb;
	private bool isGrounded;

	public GameObject target;
	// target is the object you will take the rotations from

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

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

		if (isGrounded == true) {
			rb.AddForce (moveVertical * target.transform.forward * speed);
			rb.AddForce (moveHorizontal * target.transform.right * speed);

			if (Input.GetKeyDown ("space")) {
				rb.AddForce (0.0f, jump, 0.0f);
			}
		} 
		else {
			rb.AddForce (moveVertical * target.transform.forward * speed * 0.5f);
			rb.AddForce (moveHorizontal * target.transform.right * speed * 0.5f);
		}
	}
	void OnCollisionStay(Collision collision) {

		if (collision.gameObject.tag == "Ground") {
			
			isGrounded = true;
			//print ("Grounded");
		}
	}
	void OnCollisionExit (Collision collision) {
		
		if (collision.gameObject.tag == "Ground") {

			isGrounded = false;
			//print ("Not Grounded");

		}
	}
}

the script that im referencing the rigidbody in looks like this: gyroScript.cs

using UnityEngine;
using System.Collections;

public class gyroScript : MonoBehaviour {

	public GameObject target;
	public float speed = 10f;
	public float rotationSpeed = 100.0f;
	private Vector3 offset;


	public PlayerController follower;
	public float giveaway;
	public float playerSpeed;


	void Start () {
		offset = transform.position - target.transform.position;
		follower = GetComponent<PlayerController> ();

	}

	void Update()
	{
		mouseOrbit();

	}

	void LateUpdate () {
		transform.position = target.transform.position + offset;
            //***
		playerSpeed = follower.rb.velocity.magnitude;
            // this is where i get my error***
		print (playerSpeed);
	}

	void mouseOrbit()
	{
		float translation = Input.GetAxis("Mouse Y") * speed;
		float rotation = Input.GetAxis("Mouse X") * rotationSpeed;
		translation *= Time.deltaTime;
		rotation *= Time.deltaTime;
		transform.Translate(0, 0, translation);
		transform.Rotate(0, rotation, 0);
	}
}

ive checked all my references to ensure that they are correct, infact i actually took that code directly from code i placed onto a camera object that worked without error that looked like this: CameraController.cs

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {

	public PlayerController target;

	public float giveaway;
	float playerSpeed;

	void start(){
		target = GetComponent<PlayerController>();
	}

	void Update (){

		playerSpeed = target.rb.velocity.magnitude; //same codebit that causes issues in other script
		print (playerSpeed);

		transform.position = new Vector3 (0f, 1.25f, playerSpeed * giveaway *-1); 
		print (transform.position);
	}
}

i aplologize for the long reads, but i just cant seem to figure out where im going wrong in this script, everything seems like it should work in theory

any help is much appreciated!

Hello, @andycodes!

In CameraController.cs, your Start function is declared with the lowercase s. Which explains why it worked and didn’t for gyroController.cs. Now, let’s find out what your issue is.

Here’s a portion of gryoController.cs:

	public PlayerController follower;
	...

	void Start() {
		...
		follower = GetComponent< PlayerController >();
	}

You have two ways of setting the value of follower.

  1. You can set it in the inspector.
  2. Or within function Start ( follower = GetComponent< PlayerController >(); )

I am sure you set it in the inspector, that’s how it your camera (with CameraController.cs attached to it) was able to get access to the Player Controller component of your player object - with a “misspelled” Start function (lowercase s).

The problem is that the value of follower is set to null whenStart executes because gryoScript is not attached to the same game object as PlayerController script. So it’s unable to get the component PlayerController.

Here are some solutions:

  1. Set the value of follower in the inspector and remove the linefollower = GetComponent< PlayerController >(); from the Start function of gryoScript.cs.

  2. Attach gyroScript script to the same object as Player Controller script. Letfollower be initialized in in Start function.

  3. If you don’t want gyroScript be attached to same game object as the Player Controller component. You need to declare a new game object variable in it that references the player object and initialize follower by getting Player Controller component from the newly declared game object. Like this:

       public PlayerController target;
    public GameObject player;
    
    void Start() {
    	target = player.GetComponent< PlayerController >( );
    }
    

The problem should be resolve. If there any questions, let us know!