I run into this at least once a week on the large established codebase I’m working with, and it makes debugging painful: something will move (transform.position changed), and I have no idea what moved it.
In C++, I’d use a data breakpoint to pause execution at the moment the position changes then look at the call stack. (Data breakpoints aren’t supported in Unity)
In AS3, I’d turn position into a getter/setter function and add a stacktrace just before it is set. (Transform.position can’t be overridden in Unity)
Any new ideas for figuring out what line of code is moving an object?
I’ve been in the same boat and put together a tool that weaves IL into your assembly that essentially redirects all calls from transform.postion = (or rotation/scale) to Interceptor.Set(transform, newValue) - this is then displayed in a easy to use GUI.
Tool in action on Youtube
That’s available on Asset Store
There’s also an open source component that can help:
PS: I know this thread is bit old but it still comes up when searching, thought it’d be good to share here.
Unity’s deep profiler can help you. Setting position calls some internal functions and it is not that cheap. If you do not have that many scripts and the position changes every frame, you will be able to spot it there.
Alternatively you can add Debug.Log line before each transform changing line. It will show the full trace of the call. But I believe if your codebase is large, it will be painful to look through each file. If you are on Linux or Mac, you can do something like egrep -lir --include=*.{js,cs} "(transform.position =)" . inside the root of the project using terminal. On Windows, something similar should be possible.
Another option is to disable one component after another and see if something happens.
Unfortunately, the internal call OnTransformChanged is not exposed so you will not be able to use that.
This is not a great solution, but I can override a component’s transform with the new keyword. This only applies when the component is referenced as its subclass (MyClass), but not when referenced as its parent class (MonoBehavior), so it only works if the method I’m looking for calls MyClass.transform.position not MonoBehavior.transform.position. And it catches all references to transform which is a lot. But it’s a start.
new public Transform transform {
get {
Debug.Log("====== Fetching transform");
return base.transform;
}
}
I tried using the new keyword to replace Transform with a dummy wrapper class so I could drill down specifically to position.get calls, but new Transform() seems to always return null in Unity.