"Null Object" but it IS there.

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!!!

Best way to solve a null reference error is to Debug.Log everything.

The error is pointing to line 58. Line 58 has bulletPool[nextBullet]. Immediately before this insert Debug.Log (bulletPool[nextBullet]). This will tell you if the bullet actually exists.

I suspect something is destroying your bullets. You can fix this by checking if the bullet is null, and then instantiate a new bullet to go in the gap.

There are other, more robust ways of object pooling that don’t fail if an object is deleted.

Anything deriving from UnityEngine.Object uses a different comparison operator than what you might expect. Essentially, one of these objects will equate to null if it has been destroyed, or - crucially - disabled.

Yes, you read right. A disabled object will equate to null, even though you can perform operations on it perfectly well.

The question now becomes: how do I really check if an object exists? You can cast it to an ‘object’ (lowercase ‘o’) first, which will use the standard .net operator.

However - if the object has been Destroyed, it won’t necessarily equate to null using this method. So be cautious. If you’re sure an object exists, but it might be disabled, this will work just fine.

UnityEngine.Object myObject;
if (myObject == null) // Is the object allocated, existant and active?
if ((object)myObject == null) // Is the object allocated?

If you really need to test whether an object exists, and you think it could be destroyed between the time you acquired the reference and the time you need to use it, try this:

bool objectExists = false;
try
{
    var test = myObject.hideFlags;
    objectExists = true;
} catch { }

If the object does exist, the code executes just fine. But if the object has been destroyed, Unity will throw an exception when you try to get its hideFlags, and the variable will stay ‘false’.