Detecting variable name

I’m just curious if it’s possible to detect a variable name.

For example:

	public GameObject var1;
	public GameObject var2;
	// Use this for initialization
	void Start () 
	{
		ThisIsAFunction(var1);
		ThisIsAFunction(var2);
	}
	public void ThisIsAFunction(GameObject x)
	{
		if(x.NameOfVariable == "var1")
			Debug.Log("It's var1!");
		else if(x.NameOfVariable == "var2")
			Debug.Log("It's var2!");
	}

Obviously .NameOfVariable doesn’t actually exist, I’m just using it to explain what I’m trying to do. I just want to know what the name of the variable is that I’m passing to a function. Google-fu didn’t turn up any results. Thanks for any and all help!

In unity each game object can have a name so if you want to run x.name == “var1” as long as that game object is named var1 then you will get true.
As for checking to see if a variable exists inside of a class then I do not believe that is possible.

You can access any member of any assembly through the Reflection API in .NET, including class properties. You can also very easily determine what type any member is and what qualifications any given member has (e.g. Is it static? Is it private? etc.)

Simple example…

C#

using System;
using System.Reflection;
using System.Diagnostics;

public class ReflectionExample
{
    private static string MyField;

    // Try changing this field from private to public, 
    // or remove the static qualifier, and then see if the 
    // program's output changes.

    public ReflectionExample()
    {
        MyField = "Hello World!";
    }
}

public class ReflectionTest
{
    ReflectionExample re;

    public ReflectionTest()
    {
        re = new ReflectionExample();
        Type reType = re.GetType();

        // Find the field called "MyField" in the ReflectionExample class... 
        FieldInfo myFieldInfo = reType.GetField(
            "MyField", 
            BindingFlags.Public |       // Include public fields in the search
            BindingFlags.NonPublic |    // Include private and protected fields in the search
            BindingFlags.Instance |     // Include instanced members in the search
            BindingFlags.Static         // Include static members in the search
            );       

        if (myFieldInfo == null)
            Debug.WriteLine("No field called MyField exists in the ReflectionExample class that matches the search parameters.");
        else 
        {
            Debug.WriteLine("MyField type is " + myFieldInfo.FieldType);
            if (myFieldInfo.IsPublic)
                Debug.WriteLine("MyField is public.");
            if (myFieldInfo.IsPrivate)
                Debug.WriteLine("MyField is private.");
            if (myFieldInfo.IsStatic)
                Debug.WriteLine("MyField is static.");
            else
                Debug.WriteLine("MyField is not static.");
            string myFieldValue = myFieldInfo.GetValue(re) as string;
            Debug.WriteLine("MyField = " + myFieldValue);
        } 
    }
}

// output:
// 
// MyField type is System.String
// MyField is private.
// MyField is static.
// MyField = Hello World!

Refer to MSDN for the full documentation:

Awesome! Thank you for the responses and example!

@Brian
Thanks dude, I never knew you could actually get variable names and such by using reflection.

I’m so close, but I still haven’t gotten quite there. In the following code, the Debug.Log returns “obj”, instead of the GameObject I pass to the function (Cube4).

If I use string name = GetName(() => Cube4; it works, but I’d like to use a function and pass it as a parameter.

using UnityEngine;
using System.Collections;
using System;
using System.Linq.Expressions;

public class Demo : MonoBehaviour
{
	public GameObject Cube4;
	void Awake()
	{
		LocateVariableInScene(Cube4);
	}
	
	string GetName<T>(Expression<Func<T>> expr) 
	{
	  return ((MemberExpression)expr.Body).Member.Name;
	}
	
	public void LocateVariableInScene(GameObject obj)
	{
		string name = GetName(() => obj);
		Debug.Log(name);
		obj = GameObject.Find(name);
	}
}