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 /
avatar image
1
Question by craftingtycoon · Apr 06, 2018 at 11:38 AM · variablestringget

Help with GetType().GetField().GetValue()

Hello People!

I've find a solution to read variable values from a string, but my code does not work... its have error all time, what I'm missing? or What is wrong?

 this.GetType(int).GetField("Resource"+LoadedResource).GetValue();

LoadedREsource is a string name, and when i use

 Debug.Log("Resource"+LoadedResource)

Is exactly what i need, but this is not the problem, the line remains with the red mark of "incorrect".. Anny help?

Thanks guys!

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 tormentoarmagedoom · Apr 06, 2018 at 11:41 AM 1
Share

check answer :D

1 Reply

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

Answer by tormentoarmagedoom · Apr 06, 2018 at 11:41 AM

Good day craftingtycoon !

I think you are missing the object in the GetValue. You need to say what object contains the variable. In this case i supose is "this". So you need this:

 this.GetType(int).GetField("Resource"+LoadedResource).GetValue(this);

Aaaand in case you want to SetValue(), you will need to write the GetValue sentence inside the SetValue() like this:

 this.GetType(int).GetField("Resource"+LoadedResource).SetValue(this.GetType(int).GetField("Resource"+LoadedResource).GetValue(this), NewValue);

If hellped accept the answer :D

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 craftingtycoon · Apr 06, 2018 at 11:42 AM 0
Share

Wow thanks for quick response, i dont have the red mark now, i supose it will work, we will see :D

Thank you!

avatar image Bunny83 · Apr 06, 2018 at 12:20 PM 1
Share

Uhm your second code snippet doesn't make much sense. SetValue expects the actual object just like GetValue but in addition the new value to which the field should be set to. Passing the value as object makes no sense. GetValue as well as SetValue need the same object reference. Specifically an object of the type from which you got the FieldInfo from.


So inside the same class you would do

 var fi = this.GetType().GetField("Resource"+LoadedResource);
 
 var value = fi.GetValue(this);
 
 fi.SetValue(this, newValue);

Though i would generally not recommend such an approach. The class design is flawed if you need to use reflection. It's better to represent a resource with a simple serializable class and use a dictionary to lookup a specific resource. It's faster and more flexible.

avatar image PizzaPie · Apr 06, 2018 at 02:12 PM 0
Share

Appart from that answer being wrong, the whole concept is plainly counterproductive, regarding the fact you are working inside the class the field is, using reflection to manipulate them, unless you generate code on runtime. this and reflection shouldn't be used together and as a rule of thumb reflection should be avoided on runtime.

GetType() has no overload to support the GetType(int) which would again make no sense. Cheers.

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

83 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

Related Questions

Get Variable From String 1 Answer

Public variable hidden in the inspector 2 Answers

Is there a way to get the symbolic String name [editing name] of a variable at runtime? 1 Answer

C# Variable 4 Answers

GUI text label using variable values and text 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