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 /
avatar image
0
Question by Herman · May 06, 2011 at 04:01 PM · poolobjectpoolbullet-marksrecycle

Freeze lag with object pool/recycler, why?

I am firing bullets using raycasts and I am placing bullet holes consisting of planes with decals on them at the hit point of the raycast.

To avoid instantiating GameObjects during gameplay, I created a simple object pool. My ObjectPool class has an instantiateBulletHoles function that I call from from my gun script on Awake() that creates a specified number of bulletHole GameObjects and puts them in an ArrayList

function instantiateBullets(newBulletHole : GameObject, machinegunBulletHoleCache : int){

numberOfBullets = machinegunBulletHoleCache; machinegunBulletHoles = ArrayList(numberOfBullets);

for (i=0; i < numberOfBullets; i++){

instantiatedBulletHole = GameObject.Instantiate(newBulletHole); machinegunBulletHoles.Add(instantiatedBulletHole); } }

This seems to work fine, my bulletHole game objects are created as soon as I start the game. Then, in my OnHit function in my gun script which fires when my raycast hits something, I call the getBulletHole function of my ObjectPool, which takes the next bullet hole in my ArrayList and places it at the hit point. If there are no more available bullets, it loops back around and recycles the first bullet hole in the list.

function getBulletHole(position : Vector3, rotation : Quaternion) {

nextMachinegunBulletHole +=1;

bulletHole = machinegunBulletHoles[nextMachinegunBulletHole];

bulletHole.transform.position = position; bulletHole.transform.rotation = rotation;

if(nextMachinegunBulletHole > numberOfBullets-2){

nextMachinegunBulletHole = -1;

} }

The problem is that when I start firing I get a split second freeze lag. If I continue firing after that it keeps placing bullet holes smoothly, but if I stop firing for a few seconds and then start again, it freezes again. This happens both on my Android device and on my six core, 12gb RAM workstation (framerate drops from 600+ towards zero).

Does anyone have any thoughts on what might be causing this lag? Any pointers to what can be improved in this code is greatly appreciated.

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

2 Replies

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

Answer by kromenak · May 06, 2011 at 05:14 PM

Not sure if these are the causes of your problems, but here are a few ways you could possibly improve this code:

1) Since you know precisely the number of bullets you need to cache and that value never changes, it might be more efficient to use a built-in array to hold the bullets, like

var bulletHoles = new GameObject[numberofBullets]

2) As you are instantiating bullet holes, you can call gameObject.active = false to deactivate the bullets. Then, you just activate them as they are needed and then deactivate them when they hit their "fade out" time. If your bullets have children, you can use SetActiveRecursively to accomplish this as well.

You may have already tried this, but also take the bullet hole code out of the gun and see if you still get the issue - it could be something other than the bullet holes that cause the gun to seize up while firing.

Comment
Add comment · Show 3 · 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 Herman · May 07, 2011 at 02:50 AM 0
Share

Thanks for your reply, I tried storing the bullets in a built-in array ins$$anonymous$$d but it didn't make a difference, as a matter of fact you might have hit the nail on the head in your last line, something else entirely seems to be causing the lag.

Since we are on the subject though I have a question regarding your second suggestion, isn't activating and deactivating the bullet holes more costly than keeping them active and just moving them into position? I did it this way because I imagine there is some overhead to constantly activating and deactivating items?

avatar image Herman · May 07, 2011 at 03:18 AM 0
Share

Okay, so after some script diving I seem to have gotten rid of the lag, and it looks to me that what was causing it was a couple of Debug.Log functions I was using to check what my rays were hitting. $$anonymous$$akes sense I guess, I didn't consider the fact that it might be computationally heavy but at a high rate of fire it obviously seems to have a noticable impact.

avatar image kromenak · May 08, 2011 at 01:11 AM 0
Share

Cool, good to hear you were able to fix it. Regarding the activating/deactivating stuff, I've read that it might be more costly to activate/deactivate than not, but I think it might also depend on your platform. I know it is pretty common to use deactivate unused objects on mobile devices, for example. I guess this is because the cost of keeping it active is more than the cost of switching between active and unactive. This might not be the case on desktops.

avatar image
0

Answer by DudeWithPot · May 10, 2011 at 05:58 PM

thanks! this has helped me solve my problem. Actually i had used a print(); to output a number while my bullets get shot. this has caused a lag. thanks!

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

No one has followed this question yet.

Related Questions

Recycling particle system 1 Answer

Recycling particle emitter leaves particles in 2 locations instead of 1 3 Answers

How to control pooledObject? 1 Answer

Immortal enemies when reanimated, or pooled game objects re-activated do not respond to collisions? 1 Answer

Achieving Bullet Holes 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