# Object Pool object reference is null?

**URL:** <https://discussions.unity.com/t/object-pool-object-reference-is-null/89573>\
**Category:** Questions & Answers\
**Created:** [December 11, 2013, 9:04pm UTC](https://discussions.unity.com/t/object-pool-object-reference-is-null/89573 "2013-12-11T21:04:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Party\_Boy](https://avatars.discourse-cdn.com/v4/letter/p/94ad74/32.png) [@Party\_Boy](https://discussions.unity.com/u/Party_Boy)\
**Post date:** [December 11, 2013, 9:04pm UTC](https://discussions.unity.com/t/object-pool-object-reference-is-null/89573/1 "2013-12-11T21:04:27Z")

</div>

Hey.  
So I’m new to the whole Unity thing and basically for a college assignment I have to assign a design pattern to my final year project. Since I’m doing a game in Unity I need to apply it to this. I was going to apply it to different spells that you can select to shoot different objects.

So I followed a YouTube tutorial on how to create a ObjectPool and I thought everything was going okay, however when I start the game, upon clicking, nothing emits, and I also get an error at the bottom saying “Object reference was not set to an instance of an object”.

I created a prefab with a sphere in it. I created 2 scripts, 1 called Object Pool, the other called Projectile. My Camera has its own script called FirstPersonController that is used for movement. In the ObjectPool script I detect if the user has pushed down the mouse button, then I call the ActivatedProjectile method.

EDIT

I get 2 types of error. The first appears only once at the start of the code:

ArgumentException: The thing you want to instantiate is null.  
UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineObject.cs:104)  
UnityEngine.Object.Instantiate (UnityEngine.Object original) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineObject.cs:90)  
ObjectPool.InstantiateProjectiles () (at Assets/Scripts/ObjectPool.cs:28)  
ObjectPool.Start () (at Assets/Scripts/ObjectPool.cs:12)

The other appears multiple times as follows:

NullReferenceException: Object reference not set to an instance of an object  
ObjectPool.ActivateProjectile () (at Assets/Scripts/ObjectPool.cs:37)  
ObjectPool.Update () (at Assets/Scripts/ObjectPool.cs:20)

Here is the code I have for ObjectPool class.

```
using UnityEngine;
using System.Collections;

public class ObjectPool : MonoBehaviour {

	GameObject[] projectiles = null;
	public int numberOfProjectilesToCreate = 0;

	// Use this for initialization
	void Start () {
		projectiles = new GameObject[numberOfProjectilesToCreate];
		InstantiateProjectiles ();
	}
	
	// Update is called once per frame
	void Update () 
	{
		if(Input.GetMouseButton(0))
		{
			ActivateProjectile();
		}
	}

	private void InstantiateProjectiles()
	{
		for (int i = 0; i < numberOfProjectilesToCreate; i++) 
		{
			projectiles *= Instantiate(Resources.Load("Prefabs/prefab_projectile")) as GameObject;*

```

\_ projectiles\*.SetActive(false);_\_  
\__ }_\_  
\__ }\*\_

_\* private void ActivateProjectile()\*_  
_\* {\*_  
_\* for (int i = 0; i \< numberOfProjectilesToCreate; i++)\*_  
_\* {\*_  
_\_ if(projectiles\*.activeSelf == false)  
  {  
  projectiles.SetActive(true);  
  projectiles.GetComponent().Activated();  
  }  
  }  
  }  
 }\*\__

_Anybody have any ideas on what I’m doing wrong?_

---

<div class="post-metadata">

**Author:** ![flaviusxvii](https://avatars.discourse-cdn.com/v4/letter/f/5f9b8f/32.png) [@flaviusxvii](https://discussions.unity.com/u/flaviusxvii)\
**Post date:** [December 11, 2013, 10:09pm UTC](https://discussions.unity.com/t/object-pool-object-reference-is-null/89573/2 "2013-12-11T22:09:08Z")

</div>

For Resources.Load(“Prefabs/prefab\_projectile”) you need to have the folder “Prefabs” inside a folder named “Resources”.
