When I click a script in the Inspector, the script’s source will be highlighted in Project.
Now I want to do this inversely. When I select a script in the Project, how could I highlight the gameObjects that contain it in Hierarchy?
When I click a script in the Inspector, the script’s source will be highlighted in Project.
Now I want to do this inversely. When I select a script in the Project, how could I highlight the gameObjects that contain it in Hierarchy?
You can use the search box in the hierarchy.
–Eric
Does it really work? When I haven’t known what its name is, how could I search it by name? The scenario is: I have a script named “TriggerZone”, and I want to know “Which one(s) is the object that contains TriggerZone”?
in your “TriggerZone” script, add a line of code like this:
Debug.Log("Script attach at: " + gameObject.name );
then the Unity Console will print the GameObject name, and you use this name to search in hierarchy.
Using GetComponentsInChildren() find all the components of type you’re interested in under the particular root GameObject. You get an array of components.
Loop though the array and call EditorGUIUtility.PingObject(component). If using delay, this should highlight each component; else only the last ping will be visible. Additionally, using the Selection you can select all gameObjects referenced by the array (no delay needed, see image).
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
/// <summary>
/// Highlights components of type YourType in children of a gameObject this script is attached to
/// </summary>
/// <remarks>Author: Danko Kozar</remarks>
public class HighlightComponentsInChildren : MonoBehaviour
{
public Type ComponentType = typeof(YourType);
void Awake()
{
Component[] components = gameObject.GetComponentsInChildren(ComponentType);
List<GameObject> list = new List<GameObject>();
foreach (Component component in components)
{
EditorGUIUtility.PingObject(component);
list.Add(component.gameObject);
}
Selection.objects = list.ToArray();
}
}
1058756–39346–$HighlightComponentsInChildren.cs (791 Bytes)
Another way to do this is just to right-click on the script, e.g. TriggerZone in the Projects pane and choose “Find References in Scene”.