0

I have a GameObject in Unity that is supposed to serve as a container for some definitions.

object explorer

I'd like to access that object and retrieve the Def class instances (every object there is instance of Def general class).

So, if I have a GameObject instance, how can I retrieve all objects that are instances of specific class?

apxcode
  • 7,696
  • 7
  • 30
  • 41
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778

3 Answers3

1

You can use the GameObject.GetComponents<Def>(); to retrieve all components of Def type in the GameObject.
More info in the Unity Docs http://docs.unity3d.com/ScriptReference/GameObject.GetComponents.html

Ricardo Reiter
  • 571
  • 2
  • 12
0
public Def[] defArray;
public Defs gameobject; ///if you want to access from another class assign this your Defs gameobject from inspector

defs = Defs.GetComponents<Def>();  ///if you want access from another game object
defs = gameObject.GetComponents<Def>();  ///if Defs is attached to this gameObject
Milad Qasemi
  • 3,011
  • 3
  • 13
  • 17
0

As long as you have a GameObject reference you can use GetComponents().

Def [] list = gameObject.GetComponents<Def>();
apxcode
  • 7,696
  • 7
  • 30
  • 41