- Home /
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.
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.
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.
could you post a webplayer demo? so we can see if its only you or it effects all
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.
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!
Your answer
![](https://koobas.hobune.stream/wayback/20220613135229im_/https://answers.unity.com/themes/thub/images/avi.jpg)