- Home /
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!!
not sure if this will make any difference but try declaring it as a new vector3
It was because of the var in line 14 -_- I got it though, thank you
Answer by bloodruns4ever · May 10, 2014 at 12:42 AM
You know what I got it.... In line 14 where I have var player, simply getting rid of the var worked.
That would do it, Did you by any chance look at my answer? $$anonymous$$ight be helpful. Also there is a feature in $$anonymous$$onoDevelop-Unity that will give you coding suggestions, I'm pretty sure it would pick up this dumb error, I use the feature a lot. Its called Source Analysis and its under Text Editor in the options.
Answer by Mr.Hal · May 10, 2014 at 12:41 AM
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. :D
//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);
}
Your answer
Follow this Question
Related Questions
[I REALLY NEED HELP FAST]Help with enemy Shooting 1 Answer
Staged bullet damage? 1 Answer
Only one of all my Enemies shoots, Help please. 1 Answer
Enemy AI problem 1 Answer