A little help script I use to observe object velocity and see if my other coding works:
function Update () {
Debug.Log (gameObject.transform.rigidbody.velocity.magnitude);
}
Currently it’s assigned to a simple projectile prefab.
When it’s active, my framerate drops from over 300 fps to about 30 fps. When I remove it, framerate remains almost intact.
Is this calculation so ‘heavy’ it causes such a drastic lag and why is it affecting the build version too (since it’s used only in a Debug.Log)?
Debug.Log itself is very slow. If you’ve got that assigned to a projectile and you’re firing a bunch of them that’s probably what’s slowing things down.
It happens in the build version as well because Debug.Log is still called normally in a build version, it is not specific to the editor environment. The log messages all go into a log file in your games data directory.
at this point you better make you a custom debug message on screen using some guitext that you can toggle on and off , then get rid of it and it script once you build your final game.
by the way U3 now allow you to use mono debugger break points…
if you go with it to the real player the difference will change, cause the major point of slowness is the list that has to output all the debug trashing in realtime, which is stuttering massively if you update it the whole time
Perhaps it improved speed in your “MANY times over” because by assigning an initial value to the untyped variables allowed the compiler to infer the desired type and strongly-type/compile your code instead of resorting to run-time dynamic duck typing.
As pointed out, the Debug.Log statements are the culprit.
var rigid= gameObject.transform.rigidbody;
function Update () {
Debug.Log (rigid.velocity.magnitude);
}
This will have noticeable improvements on weaker devices, however in this case it is 99%+ the Logging that is causing the slowdown.
Also in C# you can wrap the Debug Logs in a function using the Conditional attribute and have the only exist within the Editor and have the calls themselves stripped out in the final build
[System.Diagnostics.Conditional("UNITY_EDITOR")]
public static void Throw(string message)
{
Debug.Log(message);
}
This will help prevent you from missing Debugs in the final build.