I have a bullet pool that works for the player but not for my enemy. It instates the bullets as inactive gameobjects and if I go to the scene and activate it it works fine.
But it gives this error:
NullReferenceException: Object reference not set to an instance of an object
WeaponScript.Attack (Boolean isEnemy) (at Assets/Scripts/WeaponScript.cs:58)
EnemyScript.Update () (at Assets/Scripts/EnemyScript.cs:75)
And if I check for a null object before I activate it it is null.
This is my code:
using UnityEngine;
using System.Collections;
public class WeaponScript : MonoBehaviour {
public Transform shotPrefab;
public float shootingRate = 0.25f;
private float cooldown;
public bool faceForword = false;
public int numberOfBullets = 25;
private GameObject[] bulletPool;
private int nextBullet = 0;
// Use this for initialization
void Start ()
{
cooldown = 0f;
bulletPool = new GameObject[numberOfBullets];
for (int i = 0; i < numberOfBullets; ++i)
{
bulletPool *= Instantiate(shotPrefab) as GameObject;*
-
Debug.Log("Made Bullet");*
-
}*
-
}*
-
// Update is called once per frame*
-
void Update ()*
-
{*
-
{*
-
cooldown -= Time.deltaTime;*
-
}*
-
if (faceForword)*
-
{*
-
var rotation = transform.rotation;*
-
rotation.z = 0;*
-
transform.rotation = rotation;*
-
}*
-
}*
-
public void Attack(bool isEnemy)*
-
{*
-
if (CanAttack)*
-
{*
-
cooldown = shootingRate;*
-
//if (bulletPool[nextBullet] != null)*
-
//{*
-
bulletPool[nextBullet].SetActive(true);*
-
bulletPool[nextBullet].transform.position = transform.position;*
-
bulletPool[nextBullet].transform.rotation = transform.rotation;*
-
ShotScript shot = bulletPool[nextBullet].GetComponent<ShotScript>();*
-
if (shot!= null)*
-
{*
-
shot.isEnemyShot = isEnemy;*
-
}*
-
// Make the weapon shot always towards it*
-
MoveScript move = bulletPool[nextBullet].gameObject.GetComponent<MoveScript>();*
-
if (move != null)*
-
{*
-
move.direction = this.transform.right; // towards in 2D space is the right of the sprite*
-
}*
-
nextBullet += 1;*
-
if (nextBullet >= numberOfBullets)*
-
{*
-
nextBullet = 0;*
-
}*
-
// Debug.Log("Fire");*
-
//}*
-
}*
-
}*
-
///
* -
/// Is the weapon ready to create a new projectile?*
-
/// *
-
public bool CanAttack*
-
{*
-
get*
-
{*
-
return cooldown <= 0f;*
-
}*
-
}*
}
If you can help thanks!!!