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
1
Question by Voridian · Mar 03, 2014 at 03:17 AM · c#instantiatelag

How can i stop my game from laging during instantiation? C#

basically as the title suggests, my game lags like crazy for a few frames as an object is instantiated, any way to fix this without paying $30 for PoolManager? a also heard that asset bundles are a good fix but i dont really know how to use them and id rather just make this work as is. also on a side note, when is something wrong with line 39-43 because that causes lag when maxCarrots < 10 is false and cant pick up 'pickup' the carrot object. the relevant code is: using UnityEngine; using System.Collections;

 [RequireComponent (typeof(CharacterController))]
 public class FirstPersonController : MonoBehaviour {
 
     private CharacterController cc;
     float maxCarrots = 0;
     public GameObject carrotPref;
     public Transform Home;

     // Use this for initialization
     void Start () {
 
         Screen.lockCursor = true;
 
         cc = GetComponent<CharacterController>();
 
     }
 
     void OnTriggerEnter(Collider other){
         if(other.gameObject.tag=="Bouncey"){
             superJump = 1;
         }
         if(other.gameObject.tag=="Sky"){
             superJump = 0;
         }
         if(other.gameObject.tag=="Carrot"){
             if(maxCarrots < 10){
                 maxCarrots = maxCarrots + 1;
                 Destroy(other.gameObject);
             }
         }
         if(other.gameObject.tag=="Home"){
             if(maxCarrots > 0){
                 Instantiate(carrotPref, Home.position, Home.rotation);
                 maxCarrots = maxCarrots - 1
                 ;
             }
         }
     }
 
 
     // Update is called once per frame
     void Update () {
 
         Debug.Log(maxCarrots);
     }
 }

if you need the full code just ask but its a bit longer, thanks for any help.

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 Lo0NuhtiK · Mar 03, 2014 at 03:25 AM 0
Share

Unity Forums : Simple Reusable Object Pool

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Mar 03, 2014 at 04:17 AM

Depending on which platform you build for, object pools don't reduce the instantiate lag much. At least on mobile i observed almost the same lag when activating pool objects.

As a counter question: Do you have Unity pro? Because Assetbundles and asynchronous loading are pro features.

Assetbundles need to be created by an editor script or you can use a general-purpose-script like this or this.

Assetbundles are usually load from an external resource with the WWW class, however technically you could include it as TextAsset and load it from there. I actually doubt that using assetbundles will completely remove the instantiate lag.

How "large" / complicated in the sense of vertices, triangles, (sub)gameobjects, components is your object? Does the object have a rigidbody? That could be a huge factor when you instantiate it intersecting another collider / rigidbody. That would give you a huge physics spike.

It's difficult to be more precise since we don't know much about your situation.

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 Voridian · Mar 03, 2014 at 07:21 AM 0
Share

sorry for the delay, my game is for pc, and im using unity free (want to get pro but i am poor) for some reason there are 12000 faces :S dont know why, maybe because i used a path with a circle bevel object? (blender btw). there are no subobjects listed in the hierarchy aside from the 2 meshes used. it does have a rigidbody and also does have a capsule collider. i will try decimating the mesh to see if that helps.

avatar image PvTGreg · Mar 03, 2014 at 07:51 AM 0
Share

could you post a webplayer demo? so we can see if its only you or it effects all

avatar image Voridian · Mar 03, 2014 at 09:59 AM 0
Share

its ok i mostly fixed it but thanks anyways for the replies. if anybody else has this issue, try decimating the mesh and get rid of as many faces as possible.

avatar image
0

Answer by wibble82 · Mar 03, 2014 at 10:06 AM

Hi there!

PoolManager is a very full (and good) implementation of a simple system that you can easily write yourself if you want. Basically all it does is instantiate objects up front, so they're available for use later.

I won't go into too many details, but the basic idea is that if, at the start of your game you instantiate all the things you will need later and then call SetAwake(false) on them, they'll be invisible and disabled. Then when you want one, rather than instantiate it, you just grab it from your list of preprepared ones, plonk it at the right location and call SetAwake(true) on it. Bascially all you're doing is taking all those spikes during load time rather than mid game.

If you want to recycle stuff you can do the opposite. Rather than destroy an object, call SetAwake(false) on it and return it to your list of prepared ones.

On a side note, the biggest Instantiate spike I've had is from MeshColliders that are scaled to something other than 1,1,1. This causes the engine to perform a load of work rescaling the physics mesh which chomps up lots of time!

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

24 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

Related Questions

Multiple Cars not working 1 Answer

What should I change in this instantiating a GameObject in a script using Unity? 1 Answer

Destroying Objects. 1 Answer

How do i Instantiate a prefab with specific assests included 3 Answers

touch to instantiate and move object ? 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