I don’t know C# very good so, I’m trying to make a 3d tank arena game, I made a class called bullets to store bullets fired then destroy them after their lifespan is ended, but I’m getting an nullreferenceexception at" bullets[bl].instance = Instantiate(shell, muzzle.position, transform.parent.rotation) as Rigidbody;"
using UnityEngine;
using System.Collections;
public class TurretMovement : MonoBehaviour {
private class bullet
{
public bool alive;
public Rigidbody instance;
public int life;
}
private bullet[] bullets = new bullet[200];
public Rigidbody shell;
public Transform muzzle;
public float currentAngle;
private float yPos;
private Animation anim;
public int Ammo = 5;
private int Tick = 0;
private int bl = 0;
// Use this for initialization
void Start () {
yPos = muzzle.position.y;
anim = GetComponentInParent<Animation>();
}
// Update is called once per frame
void Update ()
{
for (int i = 0; i < bl; i++ )
{
if(bullets*.life <= 0)*
{
bullets*.alive = false;*
Destroy(bullets*.instance);*
}
else
{
bullets*.life -= 10;*
}
}
Tick += 10;
if(Tick % 1000 == 0 && Ammo < 5)
{
Ammo++;
Tick = 0;
}
if (Input.GetKeyDown(KeyCode.Space))
{
if (!anim.isPlaying && Ammo > 0)
{
Ammo–;
anim.Play(“ShootAnim”);
currentAngle = transform.parent.rotation.eulerAngles.y;
bullets[bl].instance = Instantiate(shell, muzzle.position, transform.parent.rotation) as Rigidbody;
bullets[bl].alive = true;
bullets[bl].life = 5000;
bullets[bl++].instance.AddRelativeForce(new Vector3(500, 0, 0));
}
}
}
}