Search Unity

drastic frame rate drop when instantiating texture!

Discussion in 'Scripting' started by lollofly, Oct 7, 2015.

  1. lollofly

    lollofly

    Joined:
    Sep 10, 2015
    Posts:
    10
    hello everybody,
    i have a problem with one of my scripts (i guess)

    Code (JavaScript):
    1. if (Physics.Raycast (transform.position, direction, hit) && hit.collider.tag == "enemy")
    2.                     {
    3.                         hitPosition = hit.point;
    4.                         hit.transform.SendMessage ("HitByRay");
    5.                         var hitRotation = Quaternion.LookRotation(Vector3.up, hit.normal);
    6.                         Instantiate (bulletHole, hitPosition, hitRotation).parent = hit.transform;
    7.                  
    8.                      
    9.                     }
    when the raycast hit a rigid body a plase with the texture of the bullet hole is instantiate, and everything is ok, BUT when the hitted object is a moving object (like enemy AI, not just a falling cube) the frame rate drops from 40-50 to 10-15 till the gameObject bulletHole is destroyed by an other script. any idea?

    sorry for bad english
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Does the bullet hole prefab have any physics things on it? (Collider, rigidbody)

    Is there one particular script you can narrow down to having a problem - you say enemy AI, but is it objects that contain movement code like that or JUST a specific enemy AI script?

    Finally, have you tried looking at it in the Profiler?
     
    lollofly and hippocoder like this.
  3. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    1. comparing tags is slow when done too frequently.
    2. SendMessage is slow when done too frequently.
    3. Instantiate is slow when done too frequently.

    Looks like you're doing things that are known to be slow too frequently. It's ok doing them a few times per frame but in this case you're probably doing it a lot, so you'll need to optimise that sooner rather than later.

    To solve the tag, you can use a dictionary lookup.
    To solve the sendmessage you can call the function directly (dictionary can store a reference to the monobehaviour script for example)
    To solve the Instantiate you can pool them.

    Lots of solutions on the forum and web. You probably should put a counter or something in there to see just how many times you're doing this. I suspect its more than you think and tbh the real bottleneck is probably going to be that Instantiate.
     
    lollofly likes this.
  4. lollofly

    lollofly

    Joined:
    Sep 10, 2015
    Posts:
    10
    thanks!

    problem solved by deleting the Mesh Collider on the gameObject! now i'll apply the optimizations descripted by @hippocoder
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    For something like bullet holes, I would definitely look into object pooling.
     
    lollofly likes this.