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:
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;
}
}
}*_