0

So i have several gameobjects in me scene which represent different actors. Also, I have scripts to for each type of actor, and they all derive from Actor script.

public class Actor : MonoBehaviour {

//some properties

}

/// 

public class Doctor : Actor {

//some more properties 

}

only the derived scripts are attached to the gameobjects. i.e. if i have a doctor gameobject- only the Doctor script is attached to it in the inspector.

My code runs through all these actor gameobjects- and updates their properties (base + derived) from some data input. Currently im checking every gameobject if he has the specific component and then get the properties from the right script.

My objective is to ALWAYS get the properties declared by derived class from Actor, that is currently attached to the gameobject, without looking for the component. I would think something in Linq will do the trick.

Fredrik Widerberg
  • 3,068
  • 10
  • 30
  • 42
mihaa123
  • 1,012
  • 9
  • 19
  • 1
    Not clear. Doctor adds some new properties extending Actor, and you want to get only those defined by Doctor? – Oguz Ozgul Nov 03 '15 at 12:48
  • [use reflection to get a class' properties](http://stackoverflow.com/a/737156/2140173) –  Nov 03 '15 at 12:52
  • basically yes. @OguzOzgul. So i want to crosscheck if a component is fulfilling 2 things: 1- being attached to the game object and 2- being derived from actor – mihaa123 Nov 03 '15 at 12:54
  • You should use reflection and the appropriate BindingFlags. If you already implemented getting all the properties, you can just check the PropertyInfo.DeclaringType. If it is Actor, skip. If it's not and it extends Actor, update. – Oguz Ozgul Nov 03 '15 at 12:58
  • This would almost work but actor itself is a MonoBehaviour, so i will get the properties of every type except 'actor'. – mihaa123 Nov 04 '15 at 11:36
  • 1
    What about `obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)`? This will return all properties of the _deepest_ type. Maybe you need some base types. In that case you can use `obj.GetType().BaseType` and call it again using `DeclaredOnly`. This should solve your problem and I think that is what @OguzOzgul meant by _You should use reflection and the appropriate BindingFlags_. – Sebastian Schumann Nov 05 '15 at 06:36

1 Answers1

0

Sounds to me like you want GetComponent<Actor>(), GetComponents<Actor>(), or FindObjectsOfType(typeof(Actor)).

  • GetComponent searches a single GameObject and returns the first matching component, or null if no such component is found.
  • GetComponents searches a single GameObject and returns an array of all matching components, or an empty array if no such components are found.
  • FindObjectsOfType searches the entire scene and returns an array of all matching Unity objects, or an empty array if no such objects are found. This last call is fairly expensive, but full-scene searches do have their uses.

Searching for Actor components will also find Doctor components, because every Doctor is by definition an Actor.

rutter
  • 11,242
  • 1
  • 30
  • 46
  • The thing is i got several actor types derived from 'actor'. When i find 1 i dont want to check which type it is, i want to get a list of the properties immediately. As long as it is derived from 'actor' and attached to a gameobject- i want its properties. With GetComponent i need to specify the component which properties i want to get. – mihaa123 Nov 04 '15 at 11:33
  • That sounds like an unusual design choice, but you can [use reflection to get all properties of an object](http://stackoverflow.com/questions/737151/how-to-get-the-list-of-properties-of-a-class). – rutter Nov 04 '15 at 16:26