- Home /
How to find out to which GameObjects a certain Script is applied?
It is easy to see which Scripts are assigned to a specific GameObject, but not vice-versa:
which GameObjects are connected to a certain Script. So I want to click on the Script and see which GameObjects or prefabs are using it.
The only way I found so far is to browse all GameObjects and its parts to see what Scripts are attached. But if there are multiple and/or complex Objects in the Scene it becomes very hard.
Same question with the public variables overrides from the Unity. Maybe there is any indication that something like this was done in Unity and I missed it?
Thanks in advance!
Answer by Steven-Walker · Mar 07, 2012 at 01:48 AM
One thing you can do is use the search field in the Hierarchy view. If you enter the full name of the script/component, only objects containing it will be listed in the view. It would be nice if this worked in the Project view too, but it does not, so you'll have to do it scene by scene.
Steven Walker, what version of Unit do you use? I tried to search in the Hierarchy and it doesn't work at all in Unity 3.4 Thanks for your answer.
rutter, it looks like there is no other way. Thanks for the answer.
Be sure you enter the component name exactly and fully without spaces. For example, you would type "$$anonymous$$eshRenderer" to list only objects with a $$anonymous$$esh Renderer component. I'm using Unity 3.5, but I've used this feature for quite a while before that.
I've just confirmed that this works fine for me in 4.2.1. It looks like it's not working, because the list narrows down to nothingness as you type, but when you type the last character, scripts with that exact component miraculously reappear.
It works only for the objects currently in the hieracrchy, but not for prefabs =/
Answer by QBob · May 22, 2015 at 12:08 PM
In Unity 5 just right click on a script and click "Find References In Scene". It will add the search term to the search bar in the Hierarchy view. Just clear this string to see all the scenes game objects again.
Answer by rutter · Mar 07, 2012 at 01:25 AM
I haven't tried it, personally, but the Unify wiki offers this SelectByComponent script. If that doesn't work, you could try using something based on the SceneDumper script.
If I had to do it from scratch, I'd probably start by calling Object.FindObjectsOfType(), and then iterating over the returned list. That looks like about what the SelectByComponent script is doing, at a glance.
Answer by aemason · May 09, 2012 at 08:58 PM
Easy just add this in your scripts start method
print ("I'm Attached to " + this.gameObject);
That was what i thought at a first glance, but read the question carefully. He wants all gameobjects that have a certain component attached, so FindObjectsOfType is the best solution.
Looks like he's asking to see all game objects a certain script is attached to. Every game object that is in a scene would be listed in the console using this line of code. You can do the exact same thing easily with FindObjectsOfType(), iterate through and print names of game objects.
Answer by dwitee · Sep 30, 2014 at 07:27 AM
you can put break point in the script's awake or start function and then in the watch window see the value of " this. gameObject"
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Cloned Prefab 3 Answers
How to check connection? 1 Answer
Create customized prefab at runtime and save it 1 Answer
Cannot destroy objects 0 Answers