- Home /
How to get to the Editor to display a child's World Position?
I've been working in Unity for 6 months, but still haven't figured this out!
How do I get the editor to display a Child Game Object's world position? If I right click on the Inspector View and select "Debug" then I can see the object's Local Position, but there does not seem to be any listing of the World Position.
I am currently working around this by simply dragging the GO out of the hierarchy into the root of the project to grab the number for my code. I realize there are a lot of other workarounds to this problem as well, but I feel like I must be overlooking how to do this in the editor.
Answer by Peter G · Dec 22, 2010 at 08:06 PM
You could make a custom editor that displays the world position. Here's a simplified example. You would probably want to do something a little more complicated since this overrides the default view.
//WorldPosViewer.js
@CustomEditor(Transform)
class WorldPosViewer extends Editor {
function OnInspectorGUI() {
EditorGUILayout.BeginHorizontal ();
target.position = EditorGUILayout.Vector3Field("World Pos", target.position);
//this will display the target's world pos.
EditorGUILayout.EndHorizontal ();
}
}
This code is fairly simple. It just displays a Vector3 field in the Transform Component instead of the standard local coordinates. The problem I spoke of earlier is that you need to add back in the functionality for viewing the rotation and other properties since this overrides them.
You could also inspect a different component that you know your object will have then indirectly access its transform.
target.transform.position = EditorGUILayout.Vector3Field();
I was originally using the syntax below to display most of my custom editors, until today I was creating one for a variable that represented an objects local transform position. The syntax I was using was displaying inconsistent values when extending the editor. I've used the syntax you listed before, but found today that in the case of local transform variables for some reason this seems to be more accurate. I can speculate as to why this happens , but anyway if you're extending local transform variables perhaps this will be useful for you too. Thanks Peter.
SerializedProperty localPositionValue = serializedObject.FindProperty("localPositionValue ");
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(localPositionValue , true);
if (EditorGUI.EndChangeCheck())
serializedObject.Apply$$anonymous$$odifiedProperties();
Answer by yoyo · Dec 23, 2010 at 04:19 AM
You can easily add new editor menu items for this sort of debug functionality. Put this C# script in your Assets\Editor folder (or a sub-folder of Editor). You should now have a new Debug menu on the main menu bar, with a Print Global Position menu item that displays the position of the selected object.
using UnityEngine;
using UnityEditor;
public static class DebugMenu
{
[MenuItem("Debug/Print Global Position")]
public static void PrintGlobalPosition()
{
if (Selection.activeGameObject != null)
{
Debug.Log(Selection.activeGameObject.name + " is at " + Selection.activeGameObject.transform.position);
}
}
}
thanks for this solution, it's more convenient than to write editor for every class.
This script is great, but it is causing my cloud build to fail with the error [Unity] Assets/GlobalPositionDebu.cs(2,7): error CS0246: The type or namespace name 'UnityEditor' could not be found. Are you missing an assembly reference?
When in doubt, comment it out!
That's because UnityEditor is not present when you build. The UnityEditor code blocks in your code, you should wrap with e.g.
#if UNITY_EDITOR editorcodehere #endif
It'll get ignored when you build.