How to Set a variable on script with a string, and value.

Hi all.

I hopefully have a pretty basic question for someone. What I want to do is to Set a variables value based on the variables name and a value. how I have been trying this is to have a function on the script with the variable as fallows.

 public Void SetInt (string _name, Int _value){

    /* would be something like*/ this.getcomponent<script name>().??? _name ???  = _value.

}

I probably will find a way because the animator has this exact thing with

 animator.SetInt(string name, int val);

Just couldn’t find the answer on the forums.

Unity does use reflection a bit, and the Animator may be doing that. However, it may also be hashing the values which would be a lot faster.

Reflection is slow, but if you’re only using it occasionally you will be fine. If you are frequently updating then it will cause some slowdown. Id also avoid using it for mobile devices as much as possible.

Depending on what you’re trying to do, setting a dictionary or other hashtable may be the best bet. You might also be able to solve the problem through proper use of class inheritance and / or interfaces.