Using an object pool with Javascript.

So I’ve been trying to figure out how to create and use a gameobject pool for at least two months now with no luck. I never thought something this common would be so difficult to figure out. The closest I have come to getting it is using this tutorial:

Object Pools

Unfortunately, it’s in C# and I am still new to scripting so I’m not sure how to convert some of this to Javascript. I think I have gotten most of it, but a few lines still escape me. Using the script below as my ObjectPoolScript, I get the error: “The thing you want to instantiate is null.” I also have another script (SquirrelScript) controlling the prefab activating it and deactivating it. Any suggestions for how to fix this or suggestions of a different (simpler) way to go about this would be hugely appreciated!

var squirrels : GameObject[] = null;
var numberOfSquirrelsToCreate : int = 0;


function Start()
{
	squirrels = new GameObject[numberOfSquirrelsToCreate];
	InstantiateSquirrels();
}

function InstantiateSquirrels()
{
	for(i = 0; i < numberOfSquirrelsToCreate; i++)
	{
		squirrels *= Instantiate(Resources.Load("Prefabs/prefab_squirrel")) as GameObject;*

_ squirrels*.gameObject.SetActive(true);_
_
}_
_
}*_

function OnMouseUp()
{
* squirrels.GetComponent(SquirrelScript).Activate();*
}

function SetSquirrel()
{
* for(i = 0; i < numberOfSquirrelsToCreate; i++)*
* {*
_ if(squirrels*.active == false)
{
squirrels.SetActive(true);
squirrels.GetComponent(SquirrelScript).Activate();
return;
}
}
}*_

I spotted a couple of things. First the only way I can see you get a “The thing you want to instantiate is null” error is if the Resources.Load() is failing. Typically when this error crossed the list, the issue is placement. Given the path you specify, the prefab must be at:

Assets/Resources/Prefabs/prefab_squirrel

Line 16 is a bit weird. Since squirrels is an array of game objects, you can do (as you did on line 31):

squirrels*.SetActive(true);*

Note if your prefab game object is active/enabled, there is no reason to enable it on load.