How to tell what method is moving an object?
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?
Answer by ChrisHandzlik · Feb 01, 2020 at 05:23 PM
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.
That's available on Asset Store
There's also an open source component that can help: https://github.com/handzlikchris/Unity.TransformSetterInterceptor
PS: I know this thread is bit old but it still comes up when searching, thought it'd be good to share here.
Woo. Spooky. #1 thread on Google that addresses my question's most recent answer 24 $$anonymous$$utes ago...... first in over 3 years. Thanks Chris. I'll give this a shot. :)
Answer by AurimasBlazulionis · Dec 16, 2016 at 08:07 PM
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.
Yeah the codebase is far too large and complex to use the profiler, check every reference to transform.position (in Visual Studio ctrl+shift+f searches all files) or disable every component. Just too many interconnected things and errors that cascade into other errors.
Answer by sarahnorthway · Dec 16, 2016 at 08:41 PM
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.
Just as a note, calling new Transform does just that. So, unless it's given a value immediately, that will always have a null output. Yes, it's annoying.
Your answer
Follow this Question
Related Questions
How do you access the transform of a gameobject and put it in a variable? 3 Answers
how to move the enemy in the current position of the player but only ones? 0 Answers
Camera isn't move position? Why my camera isn't change position? 0 Answers
Instantiate not spawning at correct position 1 Answer
I'm making a simple platformer and I want the position to reset when you fall off the screen 2 Answers