x


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 ?

more ▼

asked May 02 '11 at 08:46 PM

Agustin Petrini gravatar image

Agustin Petrini
114 6 7 12

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?

May 02 '11 at 08:50 PM Chris D

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.

May 04 '11 at 12:07 PM Agustin Petrini
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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

more ▼

answered May 02 '11 at 09:53 PM

DaveA gravatar image

DaveA
26.8k 153 171 257

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.

May 04 '11 at 03:37 PM Agustin Petrini
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3419
x482
x276
x141

asked: May 02 '11 at 08:46 PM

Seen: 1462 times

Last Updated: Sep 22 '11 at 05:03 PM