Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Enemy shooting player driving me crazy!

$
0
0
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!!

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>