Null Reference Help

Hey guys: I’m trying to base the direction of my projectile based off the direction of my player, but when I load it from resources it shows me a null reference, which is odd to me.

This is my code as it stands, and I am confused as to what I’m missing. I’m relatively noobish, so I know this may be an easy fix, but any help would be appreciated!

public class BulletScript : MonoBehaviour {

    GameObject sphere = GameObject.Find("Player");
    float speed = .5f;
    Vector3 direction;


    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        //launch bullet

        if (sphere.GetComponent().movedDown)
        {
            direction = Vector3.forward;
            Vector3 velocity = speed * direction;
            transform.position -= velocity;
        }
        if (sphere.GetComponent().movedUp)
        {
            direction = Vector3.forward;
            Vector3 velocity = speed * direction;
            transform.position += velocity;
        }
        if (sphere.GetComponent().movedLeft)
        {
            direction = Vector3.left;
            Vector3 velocity = speed * direction;
            transform.position -= velocity;
        }
        if (sphere.GetComponent().movedRight)
        {
            direction = Vector3.left;
            Vector3 velocity = speed * direction;
            transform.position += velocity;
        }
    }

}

The script can probably not find the Player. Try assigning the sphere variable manually via the Inspector.

You should call the GameObject.Find(“Player”) in the function Start().

You should also cache the sphere.GetComponent() in the start function in a var because it will cost you a lot to do it 4 times every frame just for this script.