Ive been following the unity tutorial on object pooling and wrote it out and copied it into my script and monodevelop says everything is allright but when i play it gives me null reference on lines 31,100,144. ive been at it for a few days now and at my level i dont know what to do. if you help just help with the object pooling, the stamina and stuff is already solved and uninvolved.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerMovement : MonoBehaviour {
//movement
public float moveSpeed=5;
public float rotateSPeed= 5.0f;
public float runSpeed=5.0f;
//pooling bullets
public GameObject bullet;
public int pooledAmmount = 20;
List <GameObject> bullets;
//private CharacterController playerController;
//stamina
public float curStamina;
public float maxStamina;
public float StamDrain;
public float StamRegen;
public float staminaBarLength;
private float timestamp;
private bool CanRun;
// fire rate
public GameObject shot;
public Transform shotSpawn;
public float fireRate=0.5f;
private float nextFire = 0.0f;
// Use this for initialization
//sprint
void Start () {
CanRun = true;
staminaBarLength = Screen.width / 6;
//newStamina= new PlayerStamina;
// playerController = GetComponent<CharacterController>();
//pooled bullets
bullets = new List<GameObject> ();
for (int i=0; i< pooledAmmount; i++) {
GameObject obj = (GameObject)Instantiate (bullets);
obj.SetActive (false);
bullets.Add (obj);
}
}
// Update is called once per frame
void Update () {
AdjustCurrentStamina (0);
InvokeRepeating ("Regen", 0.0f, 5.0f/StamRegen);
if (curStamina < 1) {
CanRun = false;
} else {
CanRun = true;
}
if (curStamina > maxStamina) {
curStamina=maxStamina;}
if (Input.GetKey("w")) {
transform.Translate((Vector3.forward)*moveSpeed*Time.deltaTime);
}
if (Input.GetKey("s")) {
transform.Translate((Vector3.back)*moveSpeed*Time.deltaTime);
}
if (Input.GetKey("a")) {
transform.Rotate (Vector3.down * rotateSPeed);
//transform.Translate((Vector3.left)*moveSpeed*Time.deltaTime);
}
if (Input.GetKey("d")) {
transform.Rotate (Vector3.up * rotateSPeed);
//transform.Translate((Vector3.right)*moveSpeed*Time.deltaTime);
}
if(CanRun==true)
{
if(Input.GetKey ("left shift")&& Input.GetKey ("w")) {
transform.Translate((Vector3.forward)*moveSpeed*runSpeed*Time.deltaTime);
Drain();
timestamp=Time.time;
}
else
{
if(Input.GetKey ("left shift")&& Input.GetKey ("w")) {
transform.Translate((Vector3.forward)*moveSpeed*Time.deltaTime);}}
Regen();
}
//shooting
if(Input.GetKeyUp("space")&& Time.time > nextFire)
{
Fire();
// nextFire= Time.time * fireRate;
// //GameObject clone =
// Instantiate(shot,shotSpawn.position,shotSpawn.rotation);
//as GameObject;
}
}
void OnGUI()
{
GUI.contentColor = Color.green;
GUI.Box (new Rect (40, 60, staminaBarLength, 20), curStamina + "/" + maxStamina);
}
public void AdjustCurrentStamina(int adJ)
{
curStamina += adJ;
if (curStamina < 0)
curStamina = 0;
if (curStamina > maxStamina)
curStamina = maxStamina;
if (maxStamina < 1)
maxStamina = 1;
staminaBarLength = (Screen.width / 6) * (curStamina / (float)maxStamina);
}
void Drain ()
{
curStamina -= StamDrain;
}
void Regen ()
{
if(curStamina<maxStamina &&Time.time > (timestamp + 5.0f))
curStamina+=.5f;
}
void Fire ()
{
nextFire = Time.time * fireRate;
GameObject obj = new ObjectPoolerScript.current.GetPooledObject;
if (obj = null)
return;
Instantiate (shot, shotSpawn.position, shotSpawn.rotation);
}
}
and here is another one that pools them
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ObjectPoolerScript : MonoBehaviour {
public static ObjectPoolerScript current;
public GameObject PooledObject;
public int pooledAmount=20;
public bool willGrow=true;
List<GameObject> pooledObjects;
void Awake()
{
current = this;
}
// Use this for initialization
void Start () {
pooledObjects = new List<GameObject> ();
for (int i=0; i<pooledAmount; i++)
{GameObject obj=(GameObject)Instantiate(PooledObject);
obj.SetActive(false);
pooledObjects.Add (obj);
}
}
public GameObject GetPooledObject()
{
for (int i=0; i<pooledObjects.Count; i++) {
if (!pooledObjects *.activeInHierarchy) {*
return pooledObjects ;
}
}
if (willGrow) {
GameObject obj = (GameObject)Instantiate (PooledObject);
pooledObjects.Add (obj);
return obj;
}
return null;
}}
the script t first makes a set number of bullets then if it exceeds it makes a new one but it just makes my bullets and doesn’t fire and gives me a null reference.