Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by JayFitz91 · Sep 08, 2015 at 10:53 AM · instantiateresetobject pool

Resetting an object when returning to an object pool?

I am using an object pool for efficiency but have only started implementing it, I was just Instantiating before.

I have my pool set up and the enemies enable and disable correctly but its causing some positioning issues when playing some of my animations.

My scene consists of an enemy that will walk toward me and when a certain distance away, start shooting from his mouth. When I attack him, the death animation plays which involves him going on his back and returning to the pool.

When I enable this enemy again, he walks towards me as normal but when he reaches his distance away and starts shooting, its like he's shooting from his back, the bullet still goes towards me but the animation is playing incorrectly.

I was just wondering if there was a way I could completely reset an object when it goes into the pool? And when it is enabled again, it would be like it was just freshly instantiated?

TL;DR

I want to reset my enemy object to its default settings when it returns to the object pool as if it was just instantiated as new.

Comment
Add comment · Show 1
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 Scribe · Sep 08, 2015 at 11:03 AM 0
Share

Theres no built in method, so its a case of setting up a function to save and set the state, then just save the state of the prefab object, and set your objects to that at runtime when it enters the pool. Unfortunately if you need to do that dynamically, i.e. without knowing all the variables, then you will probably need to use some kind of reflection. If you know all the variables that need to be reset, it's a lot simpler!

Of course the easier method, is just to solve your positioning bug through positioning...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MotoSV · Sep 08, 2015 at 05:59 PM

For the object pool I'm using when I want to recycle an existing instance from the pool I first pass it into the following method...

 private void PrepareClone(GameObject instance, Vector3 position, Quaternion? rotation, Transform parent, bool isActive)
         {
             instance.transform.position = position;
             instance.transform.rotation = rotation ?? instance.transform.rotation;
             instance.transform.SetParent(parent ?? this.transform, true);
 
             instance.SetActive(isActive);
         }

...and then I execute this line...

instance.SendMessage("OnSpawn", this, SendMessageOptions.DontRequireReceiver);

So, the first part will set the position, rotation, etc, and then I send a message to the instance which then allows that instance, or more specifically each component on that instance, to perform whatever initialisation is required to get it ready for use. As a side note, the this being passed along with the message to the instance is a reference to the pool that is managing it.

Now I don't know if this will solve the animation issue (probably not), but it will allow an object to be reset when retrieved from the pool.

Comment
Add comment · 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

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

30 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 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

Change value of a projectile that is instantiated in Awake and pooled 1 Answer

Checking if object intersects? 1 Answer

Strange cloning/ghosting problem with instantiated particles 0 Answers

Variable not being saved? 0 Answers

Object pooling? 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