How can I reference a specific script inside a GameObject from another MonoBehaviour.

So the documentation here says: "You can also expose references to other objects to the inspector. Below you can drag a game object that contains the OtherScript on the target slot in the inspector."

Now what if I have several scripts of that type in that GameObject, only the first one seems to be referenced. Is there a way to pick one from the inspector ?

Do you mean having several instances of the same script on the same game object? If so, I'm not sure what you mean...could you post some specific code?

–

Sorry if I wasnt clear. I mean having a variable in a script in GameObjectA of Type TypeB. Lets say I have GameObjectA with a couple of TypeB scripts . When I drag GameObjectB into the variable of GameObjectA only the first script is referenced. My question is if theres any way to specify which one gets referenced.

–

2 Answers

2

http://forum.unity3d.com/threads/97583-Referencing-a-script-on-a-GameObject-in-a-MonoBehaviour-in-another-GameObject

Cool. Now if there were some handy keystrokes to lock/unlock inspectors

–

You can use GetComponents or GetComponentsInChildren to get an array of them. How you distinguish them is up to you.

Hey thx 4 the reply. I was working on a mechanism for the designer to be able to configure some kind of events between GameObjects, not to get them through code.

–