- Home /
Multi similar script Components same game object
How to access a script on a gameobject that contains multiple components with similar instances of a script? I haven't done this before so thats why i'm asking! i want to access one of them
[alt text][1][1]: /storage/temp/15504-multi+scripts.png
is there an array of components? so we can differentiate them even with similar class names
"i want to access only one of them individually"
That doesn't make sense. Did you mean that you want to access only one of them, or each one one of them individually?
Answer by vexe · Sep 16, 2013 at 02:44 PM
One way to do it, since all the scripts you attach to your objects inherit from MonoBehaviour
, this means you can do something manual like (just showing variety of ways):
MonoBehaviour[] mbs = GetComponents<MonoBehaviour>();
foreach (MonoBehaviour mb in mbs)
if (mb is AtlasController)
// do something
Other shorter and better way:
AtlasController[] arr = GetComponents<AtlasController>();
and then filter the array.
Note that this will work on the gameObject that this script will attached to, so all these writings are the same:
AtlasController[] arr = GetComponents<AtlasController>();
AtlasController[] arr = gameObject.GetComponents<AtlasController>();
AtlasController[] arr = this.gameObject.GetComponents<AtlasController>();
I'd just use the first one. If you wanted to fetch the components from another object, you'd simply do:
AtlasController[] arr = otherGameObject.GetComponents<AtlasController>();
Awesome :) i will test it out! the is keyword is new to me
IT WOR$$anonymous$$S components are now listed :) the shorter method worked better thanks ALOT