SetActive is destroying objects instead of settings them as false
Ok so, I am creating a game with multiple weapons and seeing that the Instantiate method wouldn't be good to use for optimization I opted to use an object pooling system. When the player clicks the weapon fires, so it grabs a bullet from the array position, it then adds a force to it. From there the bullet calculates it's distance to the player, and once the bullet gets too far the velocity is zeroed out and the position is reset before it gets set inactive. As I continue to fire the weapon the bullets will start being destroyed causing an NullReferenceException to occur. Attached bellow is the weapon code and bullet code. I had this problem a while ago but was magically fixed on it's own overnight. Anyone have similar problems or ideas as to fix it?
Here is the Weapon code:
public GameManager GM;
public GameObject topObject;
public PlayerController Player;
public GameObject ShotSpawn;
private float fireDelta = 0.10f;
private float nextFire = 0.10f;
private float myTime = 0.0f;
public GameObject subMachinegunBullet;
public int Ammo = 100;
public Text AmmoCount;
public bool Reloading;
public bool dropped = false;
[Header("Muzzle Effects")]
public ParticleSystem MuzzleFlash;
public AudioSource audioSource;
public AudioClip shotSound;
public AudioClip reloadSound;
public GameObject bulletPoolParent;
public List<GameObject> bulletPool = new List<GameObject>();
[SerializeField]
private int poolCount;
void Update(){
myTime = myTime + Time.deltaTime;
AmmoCount.text = Ammo.ToString ();
Debug.DrawRay (ShotSpawn.transform.position, -ShotSpawn.transform.right, Color.red);
if (!GM.prevState.IsConnected) {
if (Input.GetMouseButton (1) && myTime > nextFire && Ammo > 0 && Reloading != true && dropped != true && Player.canMove == true) {
nextFire = myTime + fireDelta;
MuzzleFlash.Play();
audioSource.PlayOneShot(shotSound);
Shoot ();
Ammo--;
nextFire = nextFire - myTime;
myTime = 0.0f;
}
}
}
private void Shoot(){
Debug.Log ("Shooting");
#region Object Pool
bulletPool[poolCount].SetActive(true);
bulletPool[poolCount].GetComponent<TrailRenderer>().enabled = false;
bulletPool[poolCount].transform.position = ShotSpawn.transform.position;
bulletPool[poolCount].GetComponent<TrailRenderer>().enabled = true;
bulletPool[poolCount].transform.rotation = ShotSpawn.transform.rotation;
bulletPool[poolCount].GetComponent<Rigidbody>().AddForce(transform.right * 500f, ForceMode.VelocityChange);
}
Here is the bullet code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour {
public float Damage;
public GameObject Player;
float DistanceToPlayer = 0f;
[Header("Weapon Range")]
public float shortRange;
public float mediumRange;
public float longRange;
public float outOfRange;
void Start() {
Player = GameObject.FindGameObjectWithTag("Player");
}
void Update()
{
DistanceToPlayer = Vector3.Distance(this.transform.position, Player.transform.position);
Vector3 rayDir = GetComponent<Rigidbody>().velocity;
Debug.DrawRay(transform.position, rayDir, Color.red);
RaycastHit shotHit;
if (Physics.Raycast(transform.position, rayDir, out shotHit, 50f) && DistanceToPlayer < outOfRange)
{
if (shotHit.collider.tag == "Enemy")
{
#region Bullet Dropoff
if(DistanceToPlayer < shortRange) {
CloseHit(Damage, shotHit);
Debug.Log("Destroyed close range");
this.GetComponent<TrailRenderer>().enabled = false;
this.gameObject.GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 0);
this.gameObject.transform.position = new Vector3(0, 0, 0);
this.gameObject.SetActive(false);
} else if(DistanceToPlayer >= shortRange && DistanceToPlayer < mediumRange) {
MediumHit(Damage, shotHit);
Debug.Log("Destroyed medium range");
this.GetComponent<TrailRenderer>().enabled = false;
this.gameObject.GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 0);
this.gameObject.transform.position = new Vector3(0, 0, 0);
this.gameObject.SetActive(false);
} else if (DistanceToPlayer >= mediumRange && DistanceToPlayer < longRange) {
LongRangeHit(Damage, shotHit);
Debug.Log("Destroyed long range");
this.GetComponent<TrailRenderer>().enabled = false;
this.gameObject.GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 0);
this.gameObject.transform.position = new Vector3(0, 0, 0);
this.gameObject.SetActive(false);
}
#endregion
}
} else {
Debug.Log("Destroyed out of range");
this.GetComponent<TrailRenderer>().enabled = false;
this.gameObject.GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 0);
this.gameObject.transform.position = new Vector3(0, 0, 0);
Debug.Log(this.GetComponent<Rigidbody>().velocity);
this.gameObject.SetActive(false);
}
}
public void CloseHit(float Damage, RaycastHit shotHit) {
shotHit.collider.GetComponent<Enemy>().Hit(Damage * 1.5f);
}
public void MediumHit(float Damage, RaycastHit shotHit) {
shotHit.collider.GetComponent<Enemy>().Hit(Damage);
}
public void LongRangeHit(float Damage, RaycastHit shotHit) {
shotHit.collider.GetComponent<Enemy>().Hit(Damage -1.5f);
}