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 21, 2013 at 05:49 PM · javascriptsetactiveactivepoolingobjectpool

Objects in object pool not reactivating.

I am setting up an object pool where characters(squirrels) are stored in a homeTree object. They then walk out onto a branch and are flung into gameplay one at a time. There are a total of 6 squirrels, so no more than 6 can be active at any time. 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 activate automatically.

I have two scripts, the SquirrelScript attached to the squirrelPrefab and the LaunchScript, which manages the object pool. My problem is that the squirrels deactivate, but do not reactivate once they make it back to the homeEntrance. And if one touches the homeEntrance before all of the other squirrels have launched, it prevents the other squirrels from activating as well.

ProjectImage1

ProjectImage2

Here is the code from the LaunchScript:

 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 from the SquirrelScript 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();
                 numberOfSquirrelsHome--;
                 squirrelLoaded = true;
                 return;
             }
         }
     }
 }
 
 //Called from the SquirrelScript when a squirrel is launched
 function UnloadSquirrel()
 {
     squirrelLoaded = false;
 }
 
 //Called from the SquirrelScript when a squirrel reaches the homeEntrance
 function LoadSquirrel()
 {
     numberOfSquirrelsHome++;
 }

And here is the relevant code from the SquirrelScript:

 function Activate()
 {
     yield WaitForSeconds(0.1);
     transform.position = homeExit.transform.position;
     preparingToLaunch = true; //Prevents another squirrel from loading before the first one has reached the launchPoint
     squirrelActive = true; //Allows squirrel to move to launchPoint
     isJumping = true; //Must be true in order to launch properly
     squirrelDirection = true; //makes sure squirrel is moving to the right toward the launchPoint
 }
 
 function Deactivate()
 {
     gameObject.SetActive(false);
 }
 
 function OnMouseUp()
 {
     homeTree.SendMessage("UnloadSquirrel");
     yield WaitForSeconds(1);
     homeTree.SendMessage("SetSquirrel");
 }
 
 function OnTriggerEnter (theCollision : Collider)
 {
         if(theCollision.gameObject.tag == "homeEntrance")
     {
         Deactivate();
         homeTree.SendMessage("LoadSquirrel");
         homeTree.SendMessage("SetSquirrel");
     }
 }



projectimage1.jpg (71.8 kB)
projectimage2.jpg (87.9 kB)
Comment
Add comment · Show 6
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 meat5000 ♦ · Oct 21, 2013 at 08:02 PM 0
Share

Once you reactivate the gameObjects, have you tried manually calling their Start() routines?

avatar image SBull · Oct 21, 2013 at 08:07 PM 0
Share

That works, but unfortunately it instantiates the prefabs again causing six more to be added whenever the Start function is called.

avatar image whydoidoit · Oct 21, 2013 at 08:11 PM 0
Share

You comments are a little hard to follow - I'm not sure I actually get when UnloadSquirrel is called. But it appears that squirrelLoaded is try after the first squirrel is launched - is that what you intended?

avatar image meat5000 ♦ · Oct 21, 2013 at 08:18 PM 1
Share

If start works, take whatever it is within it that makes it work and copy it to a new function. Then call that function ins$$anonymous$$d of Start()

avatar image SBull · Oct 22, 2013 at 03:22 AM 0
Share

whydoidoit:

Sorry, I know it's kind of confusing. The player clicks on the loaded squirrel, drags it back and releases to launch it (kind of like Angry Birds). UnloadSquirrel is called when the mouse button is released. It causes the next squirrel to activate and move the launchPoint.

Show more comments

1 Reply

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

Answer by SilentSin · Oct 22, 2013 at 03:50 AM

I believe I know what your problem is.

What's happening appears to be: -> Squirrel is fired -> Gets back to the home entrance -> Disabled -> When needed again, enabled and placed at the home exit.

The problem appears to be that you are enabling the squirrel while it is still at the home entrance, which I believe would call OnTriggerEnter immediately, which deactivates it again immediately.

So all you need to do is reverse the order of these two lines:

 squirrels[i].SetActive(true);
 squirrels[i].GetComponent(SquirrelScript).Activate();

So you move the squirrel then activate it.

Comment
Add comment · Show 2 · 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 22, 2013 at 05:15 AM 0
Share

Thank you! Swapping those lines of code didn't work (when I tried that it gave me an error saying that the prefab was inactive). But you were right about the core reason it wasn't working. It was beco$$anonymous$$g active again while still inside the homeEntrance trigger, which caused it to become inactive again immediately. So I just set the function to be called when exiting the homeEntrance and it worked perfectly. Thanks again!

avatar image SilentSin · Oct 22, 2013 at 05:32 AM 0
Share

Yeah, the yield WaitForSeconds(0.1); can't work on an inactive object, sorry.

If you do want to have it triggered by OnTriggerEnter:

put transform.position = homeExit.transform.position; before the yield. add SetActive(true); between that line and the yield remove squirrels[i].SetActive(true); from SetSquirrel()

That way, the Activate() function takes care of everything the squirrel does to activate itself.

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

17 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 avatar image avatar image

Related Questions

Using an object pool with Javascript. 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Better way to keep track of pooled objects? 1 Answer

Javascript How to Activate/Deactivate GameObject Arrays 1 Answer

How do i check if all tagged objects are inactive? 2 Answers


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