Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
avatar image
2
Question by Saryk360 · Jun 03, 2018 at 09:33 PM · loopingfield

Iterate through object fields

I'm making an in game editor for my buildings, and I need to iterate through the building class's fields to instanciate fields to edit each property.

The fields are prefab, my buildings class has a number of fields that can vary in the future, and I'd like to make an editor that instanciates one inputField for each field I can edit.

How can I iterate through all fields ? I tried something like :

 foreach (var thisVar in building.GetType().GetProperties())
         {
             Debug.Log("Var Name : " + thisVar.Name + 
                       " ; Type : " + thisVar.PropertyType + 
                       " ; Value : " + thisVar.GetValue(building, null));
         }

but nothing appears in the console (and I am absolutely not sure about the usage of getValue()).

Can anyone give a hand ? :D

EDIT: Thanks to @Kciwsolb, I now know the code loops through attributes, and I have none. I like the idea of Scriptable Objects, but it doesn't solve the fact that I don't know the number of fields I might have in the future...

Comment
Add comment · Show 2
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
avatar image NoDumbQuestion · Jun 04, 2018 at 03:29 AM 0
Share

Normally people would make scriptable object to hold information and use it for Editor not find property in script.

Also check if building is null if building is empty then your editor did not find "Target"

avatar image Serellyn · Jun 04, 2018 at 12:57 PM 0
Share

Also, place another Debug.Log with some random test string to see if you're at least looping through it. Then if you know you're in the loop, also Debug.Log(thisVar) to what you're getting back.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Kciwsolb · Jun 04, 2018 at 01:16 PM

It is possible you do not have any properties in your class. Are you sure you have properties and not just public fields? I have seen people confuse the terms so it is worth asking. A property is a special method for accessing private fields. Here is an example:

 //This is a private field:
 private string name;
 
 //This is a property:
 public string Name
 {
       get{ return name;  }
       set{ name = value; }
 }
 
 //This is just a public field:
 public string alternateName;


Just making sure. If for some reason you were not using properties, here is the documentation on them.

Otherwise try just doing a Debug.Log(thisVar.ToString()); in your loop to see if it is working.

Comment
Add comment · Show 3 · 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
avatar image Saryk360 · Jun 04, 2018 at 09:11 PM 0
Share

Indeed I'm using fields, because I serialize the class to json so they need to be public fields. I'll edit the question to use proper vocabulary.

avatar image ShadyProductions Saryk360 · Jun 04, 2018 at 09:12 PM 0
Share

Just use GetFields() ins$$anonymous$$d of GetProperties then.

avatar image Saryk360 ShadyProductions · Jun 04, 2018 at 10:53 PM 1
Share

Works like a charm, thank you very much !

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

86 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

Related Questions

audio synthesizer in unity? 1 Answer

Animation Keeps Looping 2 Answers

How To Wrap A Blender Animation??? 1 Answer

How to make player death sound effect / hitsound play once when function is called? 1 Answer

Need some help with Array searching logic 1 Answer


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