Object reference not set to an instance of an object

A part of this script is suppose to change the InvokeRepeating timer depending on the player’s speed, it takes the value of the variable speed from a different script called PlayerMovement

using UnityEngine;
using System.Collections;

public class BlockSizeAndPosition : MonoBehaviour {

    public int Distance;
    public GameObject Player;
    public GameObject[] Blocks = new GameObject[9];

    private PlayerMovement MovementScript;
    private float RespawnTimer;

    void Awake() {
        MovementScript = GetComponent<PlayerMovement>();
    }

    void Start() {
        InvokeRepeating ("ChooseBlock", 1, RespawnTimer);
    }

    void Update() {
        ChangeTimer();
    }

    void ChangeTimer() {
        if (MovementScript.speed <= 2) {
            RespawnTimer = 4;
        } else if (MovementScript.speed >= 2 && MovementScript.speed <= 3) {
            RespawnTimer = 3.5f;
//rest
//of
//script

This is the error I get

This is the PlayerMovement script

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {

    public float speed;
    public float HorizontalMovementSpeed;
    public float increment;

    void Start() {
        InvokeRepeating("ChangeSpeed", 5, 5);
    }

    void Update() {
        transform.Translate (0, speed * Time.deltaTime, 0);
        Vector3 Movement = new Vector3 (Input.GetAxis ("Horizontal"), 0, 0);
        transform.position = transform.position + Movement * HorizontalMovementSpeed * Time.deltaTime;
    }

    void ChangeSpeed() {
        if (speed <= 10) {
            speed = speed + increment;
        }
    }

}

!(http://using UnityEngine; using System.Collections; public class PlayerMovement : MonoBehaviour { public float speed; public float HorizontalMovementSpeed; public float increment; void Start() { InvokeRepeating(“ChangeSpeed”, 5, 5); } void Update() { transform.Translate (0, speed * Time.deltaTime, 0); Vector3 Movement = new Vector3 (Input.GetAxis (“Horizontal”), 0, 0); transform.position = transform.position + Movement * HorizontalMovementSpeed * Time.deltaTime; } void ChangeSpeed() { if (speed <= 10) { speed = speed + increment; } } })

What are you trying to do, also please send the package makes it easier…

Dont begin variable names capital. Its a matter of convention. The error tells your that MovementScript is not assigned. Are you sure there is a MovementScript on the same GameObject as your BlockSizeAndPositionTimer?

If the MovementScript is assigned to the same gameobject, then it is probably not available at Awake. There are two solutions for this:

  1. Move MovementScript = GetComponent(); to your Start function. (This is the best practice. Awake() is not for accessing external objects.)
  2. Set MovementScript to be executed before BlockSizeAndPosition via Edit > Project Settings > Script Execution Order.