Enemy shooting player driving me crazy!

Please help me, I cannot for the life of me figure out what my problem is!

So I have a game very similar to geometry wars. Enemies spawn and you as a player move around and shoot. The enemies direction and speed are completely random (to a degree). I am now trying to make a new enemy variant that shoot the player with bullets of their own. I have the Enemy_Shoot Script attached to my enemy prefab:

using UnityEngine;
using System.Collections;

public class Enemy_Shoot : MonoBehaviour {

    public float bulletSpeed;
    public float fireRate;
    public float nextFire = 1;

    public GameObject bullet;

    Transform enemyBullet;
    Transform playerPosition;

    void Awake()
    {
        if (!playerPosition)
        {
            playerPosition = GameObject.FindWithTag("Player").transform;
        }
    }
	
	// Update is called once per frame
	void Update () {
	    if (playerPosition)
        {
            if (Time.time >= nextFire)
            {   
                nextFire = Time.time + fireRate;
                CreateBullet();
            }
        }
	}

    void CreateBullet()
    {
        // Creates a new instance of the bullet
        Instantiate(bullet, transform.position + (playerPosition.position - transform.position).normalized, transform.rotation);       
    }
}

and the EnemyBulletMovement attached to my bullet prefab:

using UnityEngine;
using System.Collections;

public class EnemyBulletMovement : MonoBehaviour
{

    public Vector3 velocity;
    public float step = 2f;
    private Transform player;

    void Start()
    {
        GameObject go = GameObject.FindGameObjectWithTag("Player");
        var player = go.transform;
        Debug.Log("Player:" + "" + player);
        Debug.Log(this.transform.position);
        Debug.Log("player's position" + player.position);
    }

    //Update is called once per frame
    void Update()
    {
        this.transform.position = Vector3.MoveTowards(this.transform.position, player.position, step * Time.deltaTime);
        Debug.Log("this.transform:" + "" + this.transform.position);
    }
}

However, I keep getting a Null Reference Exception in line 23 of the EnemyBulletMovement script. I’ve tried so many things and looked all over the internet and I just cannot figure it out -_- The bullet gets spawned, does not move, and I get that NullReferenceException. Any help would be great, thank you!!

You know what I got it… In line 14 where I have var player, simply getting rid of the var worked.

This is just an idea… but it would fix your problem.
Make a static variable on the player that is type Player and assign it as soon as the script runs via.
Then use it on the bullet script. Let me know if this opens some doors for you. :smiley:

//Somewhere not in a function on the player script
public static Player Player1;
//Somewhere in Start or OnEnable on the player script
Player1 = this;


//Then on the bullet movement script
    void Update()
    {
        this.transform.position = Vector3.MoveTowards(this.transform.position, Player.Player1.transform.position, step * Time.deltaTime);
        Debug.Log("this.transform:" + "" + this.transform.position);
    }