(C#) Referencing Variables using a Variable

Sorry if the title isn’t very descriptive, basically I want to do this in C#.

ThingProperties props;
props = GetComponent<ThingProperties>();
string property= "health";
props.property = 10;

ThingProperties has a public int health.

I was wondering if there was a way to implement such a thing easily. I thought of using a massive switch statement for every property but that would be a lot of work to maintain.

Thanks for your time!

Sincerely,
EoD

Do you really want it to be a string? If it doesn’t matter, I’d probably just do this:

props.health = 10

What you want is reflection. Sadly it’s not super easy to do in C#.

Here someone gives an example of both reflection, and an easier way using dictionaries instead.

Note: A dictionary is like an array but instead of numbers, you can use strings as the variables.
So you could write props[“health”] = 10, or props[property] = 10;

Thanks for the responses, I figured it out. For the example above I would do the following.

using System;
using UnityEngine;
public class Test : MonoBehaviour {

ThingProperties props;
props = GetComponent<ThingProperties>(); //Assuming ThingProperties script is attacted to object
string property= "health";
typeof(ThingProperties).GetField(property).SetValue(props, 10); //This will do the same thing as props.health = 10;

What Juice-Tin said is the proper way you should be doing things what you have above is reflection and although it does work is not very good practice and should be avoided.

Why is it not good practice and should be avoided?

It sort of makes the system work backwards to find out the properties you need. It may seem like a shortcut but it’s actually extreeeeeeeeeemely slow in terms of performance.

I see, thanks for the info Juice-Tin. I don’t think the Dictionary idea would be very useful tho. It is for an Object Properties script (health, damage, speed, etc) so using a Dictionary would have me writing that entire script, the places the values are used, and it wouldn’t come with the ability to easily change/set values within the Unity Inspector.

I suppose in terms of performance it would be best to use a massive if/else statement or switch for my specific problem.

Why don’t you elaborate on your problem so we might be able to help find a better solution?

Nothing wrong with using reflection, if its the best solution for your project.

Main draw backs are:

  • Its slower then accessing a variable directly. (But in absolute terms its still pretty fast. You won’t see this unless you do it a thousand times per frame)
  • Typos in strings can be easy to make and hard to debug

But there are plenty of cases where its worth using. Anytime you need to access a variable without knowing what it is in advance. Unity itself used reflection to manage both its serialisation system, and methods like SendMessage. I’ve used reflection heavily to watch variables dynamically, for example attaching a gauged or trend graph that can examine any variable.

Reflection also lets you access private variables. This can be really useful in some circumstances. You can code dangerously by grabbing methods Unity has not yet made public. Or you can access your own methods on other scripts without the need to make these methods part of the public API.

1 Like

@GroZZleR Sure, I am using this for networking communication. I am sending property updates in a comma separated string to others. The string data is then split and assigned to change the properties. I can give a more detailed example with code and such if you’d like.

However, assuming what BoredMormon has said, reflection should be alright in my case. It isn’t occurring that often, maybe 1000 times in an entire game…

It may not be the “best” in terms of performance, but it saves me a lot of time and hassle with switch statements.