variableName.doesPropertyExist???

Hello,
I have a bunch of components attached to an object. Each component has different properties, BUT each component has an enabled property I can set to true or false. What happens if I want to write a script that disables all the components on a certain object since ALL components contain the enabled property? Using…

var obj : GameObject;
obj.GetComponents(no arguments);  //Generic version

GetComponents with no arguments is a generic function, which SEEMS to mean to me that the Type of ‘obj’ will fulfill the determination of whatever the return type ‘T[ ]’ of the GetComponents() function will be, so in THIS case, it would return an array of components inside obj that are of System.Type ‘GameObject’ since obj is of Type GameObject.

The problem is since I want to access EVERY component no matter what Type it might be, in order to access the ‘enabled’ property that EVERY component has, how would I be able to do that? What if Unity developed a function called doesPropertyExist(name : String) : boolean and could provide a way to code something like…

//If the property 'enabled' exists in the name component
if (GetComponent("name").doesPropertyExist("enabled")) {
  //If the property 'enabled' is a boolean just like all the other 'enabled' properties in all other components.
  if (GetComponent("name").doesPropertyExist("enabled").GetType() == boolean) { 
     //Disable the component.
     GetComponent("name").enabled = false;
  }
}

The OTHER problem would be storing multiple components with multiple types into a single array in order to access and change all the components of an object that have the same property name with the same type. How would I do this?

Thank you,
Michael S. Lowe

IIRC you’re looking for something along the lines of:

GetComponents<Component>();

What if you just switch off the whole game object?

gameObject.active = false;

No. GetComponents() does not give me ALL components but only components with the name ‘component’, and gameObject.active = false; does not cover properties other than ‘enabled’. I am looking for a way to set values to all components that have the same property name and type regardless of what type each component is.

The ‘doesPropertyExist’ function is basically the kind of thing that System.Runtime.Reflection covers. However:

He means literally:

GetComponents<Component>()

which will return any component that is of type or derived from type ‘Component’ (which is all of them). Though for the ‘enabled’ property what you actually need is all components derived from Behaviour, like this:

foreach(var b in GetComponents<Behaviour>())
   b.enabled = false;

You need to either use a common base class that has the properties on it, or you need to use an interface, like this:

public interface ISomeInterestingProperties
{
   int InterestingProperty1 { get; set; }
   bool InterestingProperty2 { get; set; }
}

public class MyComponent : MonoBehaviour, ISomeInterestingProperties
{
   public int interestingProperty1;
   public bool interestingProperty2;

   public int InterestingProperty1 { get { return interestingProperty1; } set { interestingProperty1 = value; } }
   public bool InterestingProperty2 { get { return interestingProperty2; } set { interestingProperty2 = value; } }
}

var interestingComponents = (ISomeInterestingProperties[])GetComponent(typeof(ISomeInterestingProperties));
foreach(var c in interestingComponents)
{
   c.InterestingProperty1 = 5;
   c.InterestingProperty2 = true;
}