- Home /
The question is answered, right answer was accepted
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..
$$anonymous$$aybe you want to use SetValue ins$$anonymous$$d? Check the $$anonymous$$SDN for more information.
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
$$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.
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..
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);
}
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..
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
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