- Home /
how to print selected game object in editor to console?
I'm attempting to get into editor scripting but i haven't had any luck for the life of me. currently what i'm trying to accomplish is to have the console print out the name of the game object that has just been selected in the hierarchy or scene view. From what I've researched i assume I'll need the following snippet of code somewhere:
print (Selection.activeGameObject.name);
but i cant seem to figure out how to get the code running up to that point. This code i made seems to make sense to me (and i've placed the script in an "Editor" folder in my project view) but it doesn't execute.
using UnityEngine;
using System.Collections;
using UnityEditor;
public class PrintObjectName : MonoBehaviour {
void OnSelectionChange(){
print (Selection.activeGameObject.name);
}
}
If anybody could at least get me going in the right direction that'd be a tremendous help!
Answer by Yoshinator2 · Dec 02, 2016 at 03:55 AM
This is done very simply by adding
Debug.Log(Selection.activeGameObject.name);
Please accept this answer if this helped you. If you have any more questions feel free to ask. Cheers!
Hi, Yoshinator2
Replacing print with Debug.Log still doesn't appear to log the name of the gameObject to my console. Am I using the correct function [OnSelectionChange()]? $$anonymous$$y scene currently only consists of a cube called "Test Cube" and no other scripts.
Are you using a script for selection? Or are you physically clicking on it?
its an editor script that's not actively in the scene but I want it to be called by the editor whenever any game object in the hierarchy or in the scene view is selected.
Your answer
