1
\$\begingroup\$

How can we see the bounded scripts to gameobjects ? In a large project, it's hard to keep up monitoring by clicking each game object to see bounded scripts

\$\endgroup\$
2
  • 1
    \$\begingroup\$ How exactly do you want to monitor the scripts? If the built in inspector isn't cutting it, be specific about how you do want it to work. \$\endgroup\$ – MichaelHouse Mar 7 '14 at 14:47
  • \$\begingroup\$ For example I have ObstacleTrigger.cs and i bind the script to couple of game objects (obstacle types). I want to see overview of where ObstacleTrigger script is bound to. (list of which elements consist of this script) \$\endgroup\$ – Melih Mar 7 '14 at 17:06
2
\$\begingroup\$

If you want to see all the objects that are using a specific script, there are a few ways you can do that.

  • Iterate through all the game objects and return a list at will. You can find all the objects of a certain type with:

(Where myComponent is the type of component you're interested in:

myComponent[] myComponents = FindObjectsOfType(typeof(myComponent)) as myComponent[];
Debug.Log("myComponent is attached to the following game objects: ");
foreach (myComponent comp in myComponents) {
    Debug.Log(comp.gameObject.Name);
}
  • Create a script registry. Create a static class that contains a dictionary of scripts and their objects. For example:

Code to attach to some kind of scene object:

public static Dictionary<string, List<GameObject>> gameObjectsByScript 
    = new Dictionary<string, List<GameObject>>();


public void AddScript(string scriptName, GameObject gameObject) {

    if(!gameObjectsByScript.ContainsKey(scriptName))
        gameObjectsByScript.Add(scriptName, new List<GameObject>());

    gameObjectsByScript[scriptName].Add(gameObject);
}

public List<GameObject> GetGameObjectsForScript(string scriptName) {
    if(gameObjectsByScript.ContainsKey(scriptName))
        return gameObjectsByScript[scriptName];
    else
        return null;
}

Then, in all the script Start functions you can add:

ScriptList.AddScript(this.GetType().Name, this.gameObject);

How you display the results is up to your personal preference. Maybe you want to just list them in the debug window, or you want to draw boxes around them, or whatever.

\$\endgroup\$
2
  • \$\begingroup\$ This is very cool answer. Thanks. But so according to answer, i see that Unity does not have built-in tool to monitor.. \$\endgroup\$ – Melih Mar 8 '14 at 10:00
  • \$\begingroup\$ The built in tool is the inspector. Which objects are using a particular script generally isn't something people need to look up. Glad the answer was useful. \$\endgroup\$ – MichaelHouse Mar 8 '14 at 14:58
0
\$\begingroup\$

You can also right-click on a script in the Project view, choose "Find References in Scene", and it will give you a list of all GameObjects that use that Script in the Hierarchy view.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.