Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Find Original Variable type using Reflection

Discussion in 'Scripting' started by way3edgy, Nov 3, 2022.

  1. way3edgy

    way3edgy

    Joined:
    Feb 6, 2014
    Posts:
    24
    Hello, does anyone know how to find original variable types with reflection?
    I want to check if it's an Audioclip. In the code below, property.GetType() will return System.Reflection.RuntimeFieldInfo.

    Code (CSharp):
    1.  
    2. foreach(var property in this.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance))
    3. {
    4.       Debug.Log("Name: " + property.Name +" Type: "+property.GetType());
    5. }
    6.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    We know that reflection works, so rather than blast all your code into a single statement that you cannot reason about, perhaps decant it into lines so you can see what gets returned at each part based on which objects.

    Also, it may be helpful to work through some reflection tutorials, but that is a C# thing, not really a Unity thing.

    If you have more than one or two dots (.) in a single statement, you're just being mean to yourself.

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    Break it up, practice social distancing in your code, one thing per line please.

    "Programming is hard enough without making it harder for ourselves." - angrypenguin on Unity3D forums

    "Combining a bunch of stuff into one line always feels satisfying, but it's always a PITA to debug." - StarManta on the Unity3D forums
     
    way3edgy likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,525
    Variable could mean anything. It would be more useful stating what "this" is above.
     
    way3edgy likes this.
  4. way3edgy

    way3edgy

    Joined:
    Feb 6, 2014
    Posts:
    24
    Hi, yes, this is the class/script itself. I was able to find it via property.FieldType. Thanks both of you.
    For anyone with a similar problem:

    Code (CSharp):
    1.  
    2. foreach(var property in this.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance))
    3. {
    4.    if(property.FieldType ==typeof(UnityEngine.AudioClip)){
    5.        Debug.Log(true);
    6.    }
    7. }
    8.  
    This will sort through all variables declared in the class of the given type.
    P.S. could replace this.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance) with Class className to represent it. I didn't edit it above.
     
    Last edited: Nov 3, 2022
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,527
    I would highly recommend to not confuse yourself by calling a "FIeldInfo" variable "property" since there are also properties and the PropertyInfo class.
     
    Last edited: Nov 4, 2022
    Kurt-Dekker, way3edgy and MelvMay like this.