Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
4
Question by Glenno · Oct 27, 2010 at 12:01 PM · variablecomponentreflectionproperty

How do I find the name and value for each variable of a script component, using Unity script?

DaveA asked a question "Is there a way to enumerate fields (name/value) on any Component?" and in his description, he stated...

"I found a way to get all components on a GameObject. If those components happen to be Scripts, I can easily find the name/type/value for each field. Great."

But didnt explain how this was achieved, and he's using c# anyway. Could someone explain how to do this in Unity script please. Is it even possible?

Thanks, Glen

Comment
Add comment · Show 1
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 Jesse Anders · Oct 27, 2010 at 02:46 PM 0
Share

Enumerating the fields, methods, etc. of a class is certainly possible; I don't have an example handy at the moment, but look up 'c# reflection' for more info. (Or maybe someone will post some example code for you.)

2 Replies

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

Answer by Mike 3 · Oct 27, 2010 at 04:23 PM

The page you linked has an example of how to use it, you just need to use javascript syntax for the loops

e.g.

for (var script in GetComponents.<MonoBehaviour>()) { for(var mi : MemberInfo in script.GetType().GetMembers()) Debug.Log(System.String.Format("{0} = {1}", mi.Name, mi.MemberType));

 for(var pi : PropertyInfo in c.GetType().GetProperties())
     Debug.Log(System.String.Format("{0} = {1}", pi.Name, pi.GetValue(c, null)));

}

Will give you the members and properties on the scripts attached to the current object - you can look up FieldInfo and the rest of the reflection items on MSDN

You'll need to use import System.Reflection; at the top of your script for the above to work

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
avatar image
1
Best Answer

Answer by Glenno · Oct 29, 2010 at 09:35 AM

Hey all, thanks for your reply Mike. This is the code that ended up working. Yours still has some c# syntax in it doesnt it?

for (var script in GetComponents(MonoBehaviour)){ for(var mi : MemberInfo in script.GetType().GetMembers()) Debug.Log(System.String.Format("{0} = {1}", mi.Name, mi.MemberType));

 for(var pi : PropertyInfo in script.GetType().GetProperties())
     Debug.Log(System.String.Format("{0} = {1}", pi.Name, pi.GetValue(script, null)));

}

Thanks, Glenno

Comment
Add comment · Show 2 · 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 Pflegeleichtt · Jun 06, 2011 at 07:54 PM 0
Share

Hi i this error appears, when i try your script : " The name '$$anonymous$$emberInfo' does not denote a valid type ('not found').Did you mean 'System.Linq.Expressions.$$anonymous$$emberInitExpression'? " What should i do?

avatar image cbudin · Apr 15, 2014 at 03:37 PM 0
Share

Just leave out the type and it works, eg:

 for (var mi in script.GetType().Get$$anonymous$$embers())

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

2 People are following this question.

avatar image avatar image

Related Questions

how can I create a variable to refer to any gameObject script with pragma strict 1 Answer

Updating variable on another script. It is not working. 2 Answers

How to get a value from an array within another script. 1 Answer

increase a variable of a component script 4 Answers

Generic way to get properties on Component? 3 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