- Home /
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:
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[i] = Instantiate(Resources.Load("Prefabs/prefab_squirrel")) as GameObject;
squirrels[i].gameObject.SetActive(true);
}
}
function OnMouseUp()
{
squirrels.GetComponent(SquirrelScript).Activate();
}
function SetSquirrel()
{
for(i = 0; i < numberOfSquirrelsToCreate; i++)
{
if(squirrels[i].active == false)
{
squirrels[i].SetActive(true);
squirrels[i].GetComponent(SquirrelScript).Activate();
return;
}
}
}
Answer by robertbu · Oct 19, 2013 at 11:47 PM
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[i].SetActive(true);
Note if your prefab game object is active/enabled, there is no reason to enable it on load.
Ah. Thank you. However, my prefab with the SquirrelScript attached to it uses several GameObject variables. If I call the prefab from the Resources folder, it won't let me assign the GameObjects and I can't run the program if unless the they're assigned. Any ideas?
Lots of prefabs use GameObject variables, so I'm going to have to guess at what you are talking about. I suspect you have a game object in the scene that is linked to other scene game objects. If so, you have a couple of choices. First, in the script attached to the prefab you can link up these variables in Start(). Typically you would do a GameObject.Find() or a GameObject.FindWithTag() to initialize these variables. The second choice is to not make it a real prefab. You could put a variable at the top of the script like:
var squirrel : GameObject;
Then you could drag and drop a scene game object into the variable onto this variable in the Inspector. Then you would use this game object in your Instantiate().
You nailed it. I had the gameObject linked to other gameObjects, so linking them in Start() worked great.
It's almost working perfectly but there's a couple things that aren't working right that I can't figure out now. As shown below, I have the squirrels activate at the homeExit, move to the launchPoint and wait to be launched. When a squirrel is launched, the next squirrel activates and moves to the launchPoint. After launching, they then move about the level until they make it back to the homeEntrance. When they reach the homeEntrance, they deactivate. If a squirrel touches the homeEntrance and there is no squirrel waiting at the launchPoint, it should appear at the homeExit and activated automatically. I set up a system in the ObjectPoolScript to manage this.
So the two problems I'm having are:
The variable numberOfSquirrelsHome keeps track of the squirrels that are deactivated. It should go down after they are launched and up when they reach the homeEntrance. It works fine except one of the squirrels always increases the count by two. I can't figure out why it does this and it's only one of the squirrels each time that does this.
If a squirrel reaches the homeEntrance and no squirrel is loaded at the launchPoint, it should be activated at the homeExit and move to the launchPoint automatically. However it just deactivates and doesn't reactivate.
var squirrels : GameObject[] = null; var numberOfSquirrelsToCreate : int = 0; var squirrelLoaded : boolean; var numberOfSquirrelsHome : int = 6; function Start() { squirrels = new GameObject[numberOfSquirrelsToCreate]; InstantiateSquirrels(); SetSquirrel(); } function InstantiateSquirrels() { for(i = 0; i < numberOfSquirrelsToCreate; i++) { squirrels[i] = Instantiate(Resources.Load("Prefabs/prefab_squirrel")) as GameObject; squirrels[i].gameObject.SetActive(false); } } //Called when a squirrel is launched function SetSquirrel() { if(!squirrelLoaded && numberOfSquirrelsHome > 0) { for(i = 0; i < numberOfSquirrelsToCreate; i++) { if(squirrels[i].active == false) { squirrels[i].SetActive(true); squirrels[i].GetComponent(SquirrelScript).Activate(); squirrelLoaded = true; return; } } } } //Called when a squirrel is launched function UnloadSquirrel() { squirrelLoaded = false; numberOfSquirrelsHome--; } //Called when a squirrel reaches the homeEntrance function LoadSquirrel() { numberOfSquirrelsHome++; }
I don't see a problem on a quick read, but I don't fully understand how how your scripts fit together. A couple of suggestions. First, write some checks into your code. At any given time the number of squirrels in the pool plus the number of squirrels in the scene should add up to some number. Add code that checks that number any place you can. This will help you pinpoint the place where things are going wrong.
Second, UA is designed around the single question. I'm likely the only eyes still looking at this question at this point, and the content is really a new question. If you cannot figure out what is going on, open a new question so you get new eyes looking for your problem.
Okay. I will try that. Thanks for your help as usual robertbu.
Your answer
Follow this Question
Related Questions
Grabbing an object from array 0 Answers
Copying Certain Data between two Arrays 1 Answer
for loop error 2 Answers