Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by SBull · Oct 19, 2013 at 11:23 PM · javascriptgameobjectarraysetactivepooling

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[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;
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SBull · Oct 20, 2013 at 02:06 AM 0
Share

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?

avatar image robertbu · Oct 20, 2013 at 04:01 AM 0
Share

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().

avatar image SBull · Oct 21, 2013 at 12:06 AM 0
Share

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.

Picture 1

Picture 2

So the two problems I'm having are:

  1. 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.

  2. 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++;
         }
    
    
    
avatar image robertbu · Oct 21, 2013 at 03:41 PM 0
Share

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.

avatar image SBull · Oct 21, 2013 at 05:00 PM 0
Share

Okay. I will try that. Thanks for your help as usual robertbu.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Grabbing an object from array 0 Answers

Copying Certain Data between two Arrays 1 Answer

for loop error 2 Answers

Transform Checking on all Array Objects (JS) 3 Answers

Objects in object pool not reactivating. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges