• 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 kmouse · Jan 05, 2011 at 08:43 PM · rigidbodydestroylagsecond

rigidbody deleteing problem

I have a script that instantiates one rigidbody a frame. then the rigidbodys delete themselves after one second. when the rigidbodys get deleted the game lags heaps. How do you get only 10 rigidbodys to be created in a second and is there any way to delete a rigidbody without lag?

Comment
Add comment · Show 2
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 Justin Warner · Jan 05, 2011 at 08:51 PM 0
Share

$$anonymous$$ight I ask the reason for this? It's a strange question in my opinion haha.

avatar image s4vi0r · Jan 05, 2011 at 09:22 PM 0
Share

$$anonymous$$ore info plz. Can you give us an idea of why you are doing this.

2 Replies

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

Answer by Design3.com · Jan 05, 2011 at 09:01 PM

I would think that instantiating something every frame would be causing the lag, not the destruction of the objects. If you're spawning faster than you're destroying, eventually you're going to reach the limit of your CPU/memory (seconds 0-1 have 60 objects, seconds 1-2 have 119 objects, seconds 2-3 have 178 objects, etc). This is assuming you're instantiating a prefab with a rigidbody component attached to it, then using the Timed Object Destructor script provided with Unity to destroy the rigidbodies after they've been around for 1 second. This should spawn one prefab every 1/10th of a second:

var spawnInterval : float = 0.1; private var lastSpawn : float; var myRigidbody : GameObject;

function Update() { if (Time.time > lastSpawn + spawnInterval) { Spawn(); } }

function Spawn() { lastSpawn = Time.time; Instantiate (myRigidbody, transform.position, transform.rotation); }

If you're wanting to "clear the slate" every second and delete them all at once (rather than give them each a 1 second lifespan), you'd need to do something different. Maybe add each game object to an array every time you create one, and then using a similar timer as the spawning timer above, delete every rigidbody in the array each second. So something like:

var deleteInterval : float = 1.0; private var lastDelete : float; var spawnInterval : float = 0.1; private var lastSpawn : float; var myRigidbody : GameObject; private var arr : Array = new Array();

function Update() { if (Time.time > lastDelete + deleteInterval ) { Delete(); } if (Time.time > lastSpawn + spawnInterval) { Spawn(); }

function Delete() { lastDelete = Time.time; for (i=0;i<arr.length;i++) { Destroy(arr[i]); }

function Spawn() { lastSpawn = Time.time; var newRigidbody : GameObject = Instantiate (myRigidbody, transform.position, transform.rotation); arr.Add(newRigidbody); }

Though I think the feature as implemented this last way would be more laggy, since it's looping through each game object and destroying them all in the same frame/game cycle.

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

Answer by s4vi0r · Jan 05, 2011 at 09:06 PM

If you are creating 1 every frame and deleting 1 every second, then your game will more than likely crash.... eventually. Even if you limit the rigidbodies to 10:

1 second = 10 objects
2 second = 19 objects
3 second = 28 objects

you might want to manage this a different way.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

2d Endless Runner: Deleting objects behind player and platform limit 2 Answers

"Some objects were not cleaned up when closing the scene" 1 Answer

[SIMPLE] Not loose speed when destroying colliding object 1 Answer

add rigidbody when child is destroyed 0 Answers

How Do I Reduce Lag in my Networked Game? 5 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