- Home /
Unity Frame Drop From Instantiating Prefab that has script
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? Link below for video to show this....
//code 1
pragma strict
var targetA : GameObject;
var targetB : GameObject;
var setter : boolean = false;
var speed : float = 0.1;
function FixedUpdate ()
{
if(setter) {
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);
}
}
//code 2 myobject = Instantiate(moverprefab, Vector3(1025.872, 41.44104, 2492.541),Quaternion.Euler(0,29.22,0-16.8));
var theScript = myobject.GetComponent(lvl4padmover);
theScript.targetA = top.gameObject;
theScript.targetB = bottom.gameObject;
theScript.setter = true;
Why did you put your logic inside of FixedUpdate? FixedUpdate is usually used for physics only but you don't do anything physics related in there. Have you tried using the regular Update function?
Ah the fixed update when i was planning to have gravity added in for some falling pads, then I changed my $$anonymous$$d and made it move but never changed update function...
When i changed the update function my Frame Rate now runs around 20 using your Update function help, is there anything i can do to improve this, other levels where i use the same script[going through and flipping them to Update() thank you:) ] runs around 100.? Please and thank you.
Do you see any messages in the Console window as you're running this? Your code looks reasonably efficient, but maybe it would be more efficient in C# ins$$anonymous$$d of Javascript, with all your constants as explicit floats?
I have no messages in the console window when this is running, its confusing me because if I do the same thing in the level before runtime it works without issue. Only when I Instantiate the prefab do I have these issues. It is all centered on the Box Collider I believe when I disable that no issues occur, but as soon as i enable it again I drop down to 20FPS
Haven't tried this with C# since I know very little C# in combination with unity. But can one language really cause such a huge issue. And which constant would you be referring to for floats?