Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Sep 28, 2018 at 05:07 PM by hexagonius for the following reason:

this question is not specifically unity related

avatar image
0
Question by IceRevenge · Sep 28, 2018 at 02:24 PM · classesproperties

Get all the names of properties of a class,Getting property names of a class in unity

I created this class called Genes and I want to get all it's properties inside of another script. I tried using

 PropertyInfo[] props = typeof(Genes).GetProperties();

but props is an empty array. This is my class Genes:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Genes {
     private float size;
     private float speed;
     private float probability;
     private int color;
 
     public Genes(float size, float speed, float probability, int color) {
        this.size = size; 
        this.speed = speed; 
        this.probability = probability; 
        this.color = color; 
     }
 }

and I basically want to run over size, speed, probability and color using a foreach loop. What is the problem?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0

Answer by Bunny83 · Sep 28, 2018 at 03:27 PM

A property in the .NET / Mono framework is something different from a field. Your class doesn't contain any properties, only fields. So you have to use GetFields instead of GetProperties. Also get get back FieldInfo objects.

edit
I forgot to mention since your fields are private you have to use proper BindingFlags wheh using GetFields. GetFields / GetProperties without any parameters only return public and nonstatic members by default.


In most cases reflection is not the way to go. It's not really clear what you want to achieve here. The number of fields is hardcoded in the class. So you can just directly access them. Reflection should only be used when the actual class is not known. This is often the case when you create a general purpose serialization system. Be aware that reflection is slow and produces a lot of garbage. Also it has to be handled correctly. Reflection generally works with "object" values so you most likely have to cast the value to the right type.


Without more information we can't help you any further.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

91 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to make a simple quest system, but i stuck how to do 0 Answers

class design preference 1 Answer

How to list all the properties and methods from the class? 1 Answer

Array of custom properties? (C#) 1 Answer

Best programming practice for making in game menus 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges