Search Unity

Unity Frame Drop From Instantiating Prefab that has script

Discussion in 'Scripting' started by chase.willits, Jan 23, 2014.

  1. chase.willits

    chase.willits

    Joined:
    Jul 26, 2013
    Posts:
    6
    I have a prefab that I instantiate 10 seconds after loading the level. The Script is a generic mover script which as you can see will not run as default. Then I have a separate scrip to instantiate the object which works correctly and assign targetA and targetB which normally would be done in inspector but the two targets are also instantiated objects that are made before my prefab with mover script. When I run this the script runs and everything but for some reason the main thread spike to 516-520milisecond response time dropping FPS in game from 120ish to 1.5ish. Can anyone explain/help me with this? Video below is showing this....

    Code (csharp):
    1. #pragma strict
    2.  
    3. var targetA : GameObject;
    4. var targetB : GameObject;
    5. var setter : boolean = false;
    6. var speed : float = 0.1;
    7.  
    8. function FixedUpdate ()
    9. {
    10. if(setter) {
    11. var weight = Mathf.Cos(Time.time * speed * 2 * Mathf.PI) * 0.5 + 0.5; transform.position = targetA.transform.position * weight + targetB.transform.position * (1-weight);
    12. }
    13. }
    14.  
    Code (csharp):
    1.         myobject = Instantiate(moverprefab, Vector3(1025.872, 41.44104, 2492.541),Quaternion.Euler(0,29.22,0-16.8));
    2.         var theScript = myobject.GetComponent(lvl4padmover);
    3.         theScript.targetA = top.gameObject;
    4.         theScript.targetB = bottom.gameObject;
    5.         theScript.setter = true;
    6.