- Home /
GetProperties() not returning the declared properties of a script, returning only inherited properties instead.
I am trying to get a list of the properties declared within the class but the best I seem to be able to do is to find the inherited members without any of the declared ones (This can be seen if using the below code but removing 'BindingFlags.DeclaredOnly'). I would expect the below code to output the following:
Test123
scripts
_
Instead, what is returned is:
Test123
_
I'm hoping that someone will be able to set me right here.
Thanks!
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
public class Test123 : MonoBehaviour {
public MonoBehaviour[] scripts;
void Awake()
{
PropertyInfo[] properties = this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
print(this.GetType().Name);
for (int j = 0; j < properties.Length; j++)
print(properties[j].Name);
print("_");
}
}
Answer by Bunny83 · Mar 20, 2018 at 05:07 PM
scripts is not a property but a field. A property (in the sense of C# / .NET) is a set of two methods: a getter and a setter. A field is an actual data field. If you want to get the field of a class use GetFields instead of GetProperties
It's always something so simple. That it working perfectly now. Thanks for the help!
Your answer
![](https://koobas.hobune.stream/wayback/20220612155001im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Help me finish my room generator 0 Answers
How to make GUI Button a fixed size? 1 Answer
Static variable work in one script but not in the other 1 Answer