2D Bullet Not Working
I have a 2D platformer and am using C#. And I'm trying to shoot my bullet prefab out of my gun. So basically, when I shoot my gun the bullet spawns but doesn't move, it gets stuck in mid-air. And second the bullet sprite wont flip to go in the direction I shoot it, it only spawns in one direction. So my entire script that goes on my bullet prefab doesn't work. And one of the reasons is because I have a reference problem, I don't have a bullet prefab in my inspector (when I did, the bullet would fly off, it kind of worked. But also the bullet would move in the direction I would move, if I shot right and then walked left, the bullet would change directions and travel left). So my reference to the script for the bullet I have on a script attached to my player says that its null because it cant find the prefab or whatever. I don't know what to do, I've given up, I've tried everything... Please help.
SCRIPT ATTACHED TO PLAYER:
public class Glock18 : MonoBehaviour {
public float fireSpeed = 0f;
public float fireRate = 0f;
public float damage = 10f;
public LayerMask whatToHit;
public Transform firePoint;
public Transform bulletTrail;
private WeaponPickUp weaponPickUp;
private BulletTrail bulletScript;
void Awake()
{
weaponPickUp = transform.root.GetComponent<WeaponPickUp>();
bulletScript = transform.GetComponent<BulletTrail>();
}
void Update()
{
if (weaponPickUp.glockE == true)
{
if (fireRate == 1 && Input.GetButtonDown("Fire1") || Input.GetKeyDown(KeyCode.L))
{
Debug.Log("is bulletScript null? " + (bulletScript == null));
Shoot();
}
}
}
void Shoot()
{
Vector2 firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
RaycastHit2D hit = Physics2D.Raycast(firePointPosition, firePointPosition, 10, whatToHit);
Effect();
}
void Effect()
{
if (bulletScript)
{
Debug.Log("Move");
bulletScript.GetComponent<BulletTrail>().Move();
}
Instantiate(bulletTrail, firePoint.position, firePoint.rotation);
}
}
SCRIPT ATTACHED TO BULLET:
public class BulletTrail : MonoBehaviour {
public int moveSpeed = 0;
public bool startingRight = true;
private PlayerController playerController;
void Awake()
{
playerController = GameObject.FindObjectOfType<PlayerController>();
}
void Start()
{
if (playerController.facingRight)
{
startingRight = true;
}
else if (!playerController.facingRight)
{
startingRight = false;
}
}
public void Move()
{
if (!playerController.facingRight)
{
MoveLeft();
}
else if (playerController.facingRight)
{
MoveRight();
}
}
void MoveLeft()
{
if (startingRight)
{
Debug.Log("FlipL");
Flip();
}
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(Time.deltaTime * moveSpeed * -50, GetComponent<Rigidbody2D>().velocity.y);
Destroy(gameObject, 1);
}
void MoveRight()
{
if (!startingRight)
{
Debug.Log("FlipR");
Flip();
}
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(Time.deltaTime * moveSpeed * 50, GetComponent<Rigidbody2D>().velocity.y);
Destroy(gameObject, 1);
}
void Flip()
{
startingRight = !startingRight;
Vector3 scale = transform.localScale;
scale.x *= -1;
transform.localScale = scale;
}
}
Could you show us the script where the null reference error is thrown?
There is no null reference error its that the way my reference is set up causes my bullet script not to work, Its confusing. :/
Because when I had the prefab in my inspector, the script would work for that one prefab. But not the ones spawned from my "FirePoint".
Answer by TheShadyColombian · Jul 22, 2016 at 06:14 PM
Whenever you get a Null reference error (or any error, in fact) the script will stop and any code after the error will not run. Check the console and fix EVERY error (or add try/catch statements). The console will tell you what line the error is in (/path/to/script: errorLine) or just double click it, and it will place the cursor on the line with the error. try commenting that line.
Let me know what happens.
I said in my response to the other guy, I don't have a null error, I don't have any errors. It just doesn't work...
I think I see the problem. So just to clarify, the only problem with specifying the prefab in the inspector is that it sets the direction of the bullet even after being shot out of the gun?
when I shoot the gun, the bullet always fires right, the bullet sprite always faces right, and the bullet changes direction if I change direction.
Your answer
Follow this Question
Related Questions
Unity 2d simple c# shooting script 1 Answer
2D Platformer Gun Equip Script Not Working 0 Answers
How To Do Basic 2D Movement? 1 Answer
Random Number keeps generating over and over 2 Answers
Advance Colision Detection 1 Answer