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 Jul 29, 2017 at 04:30 PM by $$anonymous$$ for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by $$anonymous$$ · Jul 29, 2017 at 01:54 PM · stringvaluetypeassignoverride

How Can I Change GetType() And GetField() And GetValue() ?

Here I Have An Example Script:
-JavaScript:

 public var valueName : String = "value";
 public var value : float = 0;
 public var source : Component;
 public function Update ()
 {
     source.GetType().GetField(valueName).GetValue(source) = value;
 }

-C#:

 public string valueName = "value";
 public float value = 0;
 public Component source;
 public void Update ()
 {
     (float)source.GetType().GetField(valueName).GetValue(source) = value;
 }

Gives Me This Error:
Expression 'self.source.GetType().GetField(self.valueName).GetValue(self.source)' cannot be assigned to.
Is There A Way To Override And Change The Values From These?
I Can Only Be Able To Read Them.
Thanks..

Comment
Add comment · Show 5
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 Cherno · Jul 29, 2017 at 02:40 PM 0
Share

$$anonymous$$aybe you want to use SetValue ins$$anonymous$$d? Check the $$anonymous$$SDN for more information.

avatar image $$anonymous$$ Cherno · Jul 29, 2017 at 03:01 PM 0
Share

I'm Using SetValue() But It Gives $$anonymous$$e A Different Error:

 source.GetType().GetField(valueName).GetValue(source).SetValue(source,value);

Error: $$anonymous$$issing$$anonymous$$ethodException: System.Single.SetValue

avatar image Reynarz $$anonymous$$ · Jul 29, 2017 at 03:57 PM 0
Share

$$anonymous$$an, i have some questions, for understand how i can help you: What are you trying to do with the component?

That is the only way?

You don´t know what value is in compile time? (i know the answer).

As i can see, you are working with a float, but why using reflection? you can use an interface ins$$anonymous$$d to do all that stuff.

Show more comments

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by $$anonymous$$ · Jul 29, 2017 at 04:29 PM

I Used Another Method But It Was Based Something Like c4ctus's Answer:
Just Simply Replace GetValue With SetValue And Do It Like That:
-JS:

  public var valueName : String = "value";
  public var value : float = 0;
  public var source : Component;
  public function Update ()
  {
      source.GetType().GetField(valueName).SetValue(source,value);
  }

-C#:

  public string valueName = "value";
  public float value = 0;
  public Component source;
  public void Update ()
  {
      source.GetType().GetField(valueName).SetValue(source,value);
  }

Thanks..

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
0

Answer by c4ctus · Jul 29, 2017 at 03:47 PM

Use the SetValue method:

 public string valueName = "value";
 public float value = 0;
 public Component source;
 private FieldInfo _fieldInfoCache;
 
 public void Start()
 {
     _fieldInfoCache = typeof(Component).GetField(valueName);
 }
 
 public void Update()
 {
     if(_fieldInfoCache == null) return;

     _fieldInfoCache.SetValue(source, value);
 }
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 $$anonymous$$ · Jul 29, 2017 at 04:19 PM 0
Share

I Used Another $$anonymous$$ethod But It Was Based Something Like Yours:
-JS:

 public var valueName : String = "value";
 public var value : float = 0;
 public var source : Component;
 public function Update ()
 {
     source.GetType().GetField(valueName).SetValue(source,value);
 }

-C#:

 public string valueName = "value";
 public float value = 0;
 public Component source;
 public void Update ()
 {
     source.GetType().GetField(valueName).SetValue(source,value);
 }

Thanks..

avatar image c4ctus · Jul 29, 2017 at 06:16 PM 0
Share

I put the value on cache memory to not call multiple times the methods GetType() and GetField() because reflection reduces performance c:

Follow this Question

Answers Answers and Comments

69 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

Related Questions

how to assign the value of a string as variable 3 Answers

GetComponent via String name? 3 Answers

What is typing a variable? Is it assigning the value? 3 Answers

GetComponent and converting string to Type type 1 Answer

Get "player value" from string 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