0
\$\begingroup\$

I have a script that instantiates a object lets say every half a second. THis object is of the type .fbx and has a file size of 53kb, lets call this object A. When i use this object my script instantiates the objects without any freezes or any other problems.

Now i have an other object also of type .fbx and also with a filesize of 53kb lets call this object B. Now when i want to instatiate object my my game freezes a few frames upon instantiation.

Why? Is there a way to instatiate objects more efficiantly? I already looked at using object pooling but this doesnt really works in my case.

See here a screenshot for object A, and its components:
enter image description here

See here object B and its components:
enter image description here

Per request the code that does the instantiation of the objects:

public class ForLoopVisualization1 : MonoBehaviour {

    public DetectorAreaScript ElementDetector;
    public GameObject TowerElement;
    public Transform TowerElementsParent;
    public bool WigglyTower = false;
    public float ElementSpacing, Wiggeliness;

    private Collider BuildElementCollider;
    private Transform BuildElementTransform;
    private Vector3 TowerElementPos;
    private int TowerElementCount, CreatedElements;
    private bool StartBuildingTower = false;
    private float Delay = 0.5f, TowerElementSize;

    // Use this for initialization
    void Start () {
        ElementDetector.DetectedData += OnObjectEnterDetector;
        Destroy(TowerElement.GetComponent<Rigidbody>());
        TowerElementSize = TowerElement.GetComponent<Collider>().bounds.size.y;
        TowerElementPos = TowerElementsParent.position + new Vector3(0, TowerElementSize * 1.5f + ElementSpacing, 0); 
    }

    // Update is called once per frame
    void Update () {
        if(StartBuildingTower && CreatedElements < TowerElementCount)
        {
            Delay -= Time.deltaTime;
            if(Delay <= 0)
            {
                CreatedElements++;
                var te = Instantiate(TowerElement, TowerElementPos, Quaternion.identity);
                te.transform.SetParent(TowerElementsParent);
                if(WigglyTower) TowerElementPos += new Vector3(Random.Range(-Wiggeliness, Wiggeliness), TowerElementSize + ElementSpacing, Random.Range(-Wiggeliness, Wiggeliness));
                else TowerElementPos += new Vector3(0, TowerElementSize + ElementSpacing, 0);
                Delay = 0.4f;
            }
        }
        else if(StartBuildingTower)
        {
            StartBuildingTower = false;
            // StartCoroutine(EjectVar()); // add when bugs are fixed..
        }
    }

    void OnObjectEnterDetector(System.Object sender, DetectedVariableData data)
    {
        if(data.DetectedObjectType == DetectedType.Variable) 
        {
            var vals = ElementDetector.DetectedVariable.GetVariableValue();
            TowerElementCount = vals.IntValue;
            if(CreatedElements > 0)
            {
                foreach (Transform child in TowerElementsParent)
                {
                    Destroy(child.gameObject);
                }
                CreatedElements = 0;
            }
            TowerElementPos = TowerElementsParent.position + new Vector3(0, TowerElementSize * 1.5f + ElementSpacing, 0); 
            StartBuildingTower = true;
        }
    }

    IEnumerator EjectVar()
    {
        yield return new WaitForSeconds(0.8f);
        ElementDetector.EjectDetectedElement(new Vector3(-1, 0, -1));
    }
}

If something is unclear let me know so i can clarify the post!

\$\endgroup\$
  • \$\begingroup\$ If you instantiate them more than once, is it only the first time that causes a freeze or is it every time? Also, are you instantiating both of these in the same fun, or are you swapping out objects a and b and then running and seeing the freeze? \$\endgroup\$ – Ed Marty Dec 30 '18 at 18:58
  • \$\begingroup\$ I do instantiate more then once and it happens everytime. And yes i am swapping objects a and b and then running the game. Hope this clarifies it! \$\endgroup\$ – FutureCake Dec 30 '18 at 19:05
  • 1
    \$\begingroup\$ Have you checked the Profiler to see what it says it's going on? docs.unity3d.com/Manual/Profiler.html \$\endgroup\$ – Ed Marty Dec 30 '18 at 19:09
  • \$\begingroup\$ Does the fbx you're instantiating have any textures associated with your material? Unity loads textures to the gpu in a "lazy load" fashion (on "first demand") and it accounts for a portion of the hiccups you're experiencing. If object pooling does not suit your case, you could at least make sure your textures are preloaded (for this you could create a material with all of your textures assigned to it and assign that material to some dummy model that you place in the scene but keep it offscreen). Also, shaders are loaded the same way, you might want to look at: Shader.WarmupAllShaders(). \$\endgroup\$ – mt_ Dec 30 '18 at 19:25
  • \$\begingroup\$ But overall, it's not possible to completely remove the hiccups when you're instantiating on the fly, you can somewhat alleviate how significant it is, but you'll always see a spike in your profiler. \$\endgroup\$ – mt_ Dec 30 '18 at 19:28
1
\$\begingroup\$

So after all great help from the comments i found out it actually had to do with the meshCollider on the object. The objects that caused the problems had a really smooth surface, which results in some rather long computing times to generate the convex mesh collider. Which resulted in the little bits of lag. So i just made a custom low poly collider for the problematic objects and that fix the issues.

Hope this helps anyone. If more information is needed i am happy to help!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.