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
1
Question by DaJuice · Sep 21, 2012 at 02:02 PM · dynamicaccessing

C#, Dynamically accessing properties with a String

Hi there, to make it short, I need to set and get the value of a property of a component using a string. I found this code which is, I believe, supposed to work:

component.GetType().GetProperty("property").GetValue(component, null); component.GetType().GetProperty("property").SetValue(component, "value", null);

But it does not work, it returns absolutely nothing, when I try

Debug.Log(component.GetType().GetProperty("property"));

I did not include "GetValue", because GetProperty returns nothing, an empty line, not even a "null"

I'm one week old in C# so my knowledge is rather limited, Is there something special to do in order for it to work, or any workaround for this?

Thanks.

Comment
Add comment · Show 6
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 guto-thomas · Sep 21, 2012 at 02:45 PM 0
Share

What is the type of the 'component' variable? You need to have at least a $$anonymous$$onoBehaviour type to get a property from a script you wrote, as far as I know. Because, of course, this particupar script you wrote probably derive from $$anonymous$$onoBehaviour. Also, does it have a property called 'property'?

avatar image DaJuice · Sep 21, 2012 at 02:48 PM 0
Share

well, this was an example script, but the specific script looks like this:

public void set($$anonymous$$onoBehaviour component, string[] properties) {

Hashtable hash = new Hashtable(properties.Length); int i = properties.Length; while(i-->0){ Debug.Log(properties[i] + "-" + component.GetType().GetProperty(properties[i]) + "-");//returns nothing hash.Add(properties[i], component.GetType().GetProperty(properties[i]).GetValue(component,null));//Does not work, error because null } resetVals.Add(component, hash); }

all this is in a $$anonymous$$onobehaviour.

avatar image DaJuice · Sep 21, 2012 at 02:52 PM 0
Share

oh and when I try to use GetProperties, it returns an array of length 0, so the name of the properties should not be the problem...

avatar image FatWednesday · Sep 21, 2012 at 03:22 PM 0
Share

I had similar problems when using reflection, I found that looking for fields ins$$anonymous$$d of properties worked. have you tried that?

avatar image DaJuice · Sep 21, 2012 at 03:32 PM 0
Share

sweet! This works!! thanks alot! I don't know how this works, should you post this in Answers? Or should I?

Show more comments

1 Reply

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

Answer by FatWednesday · Sep 21, 2012 at 03:33 PM

I had similar problems when using reflection, I found that looking for fields instead of properties worked. have you tried that?

(reposting comment as answer)

Comment
Add comment · Show 4 · 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 Gurc · Apr 13, 2013 at 07:27 PM 0
Share

Worked like a charm.

avatar image Pawl · Jun 25, 2014 at 11:59 PM 0
Share

You're a life saver my friend!

avatar image gulfaraz · Dec 20, 2014 at 08:18 PM 0
Share

Thank You!

avatar image samana1407 · Jan 11, 2017 at 04:16 PM 0
Share

Wow, 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

14 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

Related Questions

Scaling Animation for a Instantiated Object 1 Answer

Google Play Services Real Time Multiplayer accept invitation from notification and launch game with invitation accepted 0 Answers

Lightmapped terrain no shadow from dynamic objects 0 Answers

Error trying to use ExpandoObject in Unity 2018.1 2 Answers

How to build a tilemap at runtime? 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