Search Unity

Question Getting Script Object Name (Class Name) As Type?

Discussion in 'Scripting' started by Studio_Akiba, Mar 2, 2023.

  1. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,426
    Hi, I'm trying to automate grabbing the public and private variables of a script, but I'm running into problems with types.

    I've managed to work out that target.GetType() gets the MonoScript declaration, and ((MonoScript)target).GetClass().BaseType gets the inherited type (Monobehavior) but I can't for the life of me manage to get it to return the actual target class name as a usable type that I can get fields from.

    I know target.name will return it, but it only does so as a string, so I can't use reflection to grab fields with that.

    Is there any way to grab an object scripts name as it's type?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,938
    You can get members, fields, property information, etc, from its System.Type. You don't need the name of the type.

    More accurately, you get Reflection values such as FieldInfo, MemberInfo, etc, from the type. To get the actual values, you need an instances to pull those values out from as well.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    SisusCo likes this.
  4. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,426
    This worked perfectly.

    It's a bit of an odd situation, I'm trying to automate getting these so they can be bound to UI elements for editing in-game.

    I have found another issue though, I don't seem to be able to get the PropertyInfo from the class, I'm not sure why but the stuff in the try-catch always fail. the pi always returns null, even though there's nothing weird in the setup of the script it's targeting.

    Code (CSharp):
    1. foreach(FieldInfo field in ((MonoScript)target).GetClass().GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static))
    2.         {
    3.  
    4.             if (field.DeclaringType == ((MonoScript)target).GetClass())
    5.             {
    6.                 try
    7.                 {
    8.                     VisuDataTargetVariables vdtv = new VisuDataTargetVariables();
    9.                     try
    10.                     {
    11.                         PropertyInfo pi = ((MonoScript)target).GetClass().GetProperty(field.Name);
    12.                         MethodInfo mi = pi.GetSetMethod(true);
    13.  
    14.                         if (mi.IsPublic)
    15.                         {
    16.                             vdtv.declaration = BindingFlags.Public;
    17.                         }
    18.                         else if (mi.IsPrivate)
    19.                         {
    20.                             vdtv.declaration = BindingFlags.NonPublic;
    21.                         }
    22.                     }
    23.                     catch { }
    24.  
    25.                     vdtv.varName = field.Name;
    26.                     vdtv.varType = field.FieldType;
    27.  
    28.                     tgtVars.Add(vdtv);
    29.                 }
    30.                 catch
    31.                 {
    32.  
    33.                 }
    34.             }
    35.         }
    I was trying to follow this, but I don't seem to be able to get it working.
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    Are you sure it's a property you're looking for and not a field? There's a major different. Also is the member you are targetting public, private, static? All of these change which BindingFlags you should be passing into your GetField/GetMember call.
     
  6. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,426
    I'm trying to grab them all, not sure about field or property, never really done this before, but from the MS docs I thought a variable was considered a property when grabbing the declaration visibility.

    I wanted to grab them all, but be able to tell whether they are public, private or static (thats what vdtv.declaration is for).
    I couldn't find much on how to do this so I've been mostly trial-and-erroring it :(
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    Properties and fields are very different things in C#. You need to be aware of what each of your members is. Most of the time if you're a beginner you are dealing with fields. They cannot be used interchangeably.
    Code (CSharp):
    1. PropertyInfo pi = ((MonoScript)target).GetClass().GetProperty(field.Name);
    Is never going to work for example, since we know field is a FieldInfo.