Search Unity

System.Type.GetType("Transform") not work

Discussion in 'Scripting' started by ilcygnet, Jan 29, 2010.

  1. ilcygnet

    ilcygnet

    Joined:
    Sep 10, 2009
    Posts:
    28
    Hi dudes, I got new problem :(
    I'm making Unity instances from XML script.
    And code below returns null.

    Code (csharp):
    1. Type type = System.Type.GetType("Transform");
    I also tried code below

    Code (csharp):
    1. Type type = System.Type.GetType("UnityEngine.Transform");
    It's still not working. It looks only Unity3D classes return null. .NET classes and custom classes derived from nothing return correct Type instance. For example codes below returns correct type information,

    Code (csharp):
    1. Type type = System.Type.GetType("System.Collections.ArrayList");
    I think the namespace of Transform is incorrect or Unity3D classes have something different reflection condition. Do you have any ideas?
     
  2. dkoontz

    dkoontz

    Joined:
    Aug 7, 2009
    Posts:
    198
    I have this same problem, did you ever find a solution?
     
  3. Rod-Green

    Rod-Green

    Joined:
    Apr 19, 2010
    Posts:
    51
    This was working for me up until today. I don't know what happened but now none of the UnityEngine types return anything but null.

    Anyone had this issue?
     
  4. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    http://msdn.microsoft.com/en-us/library/w3f99sx1.aspx
    Looks like System.Type.GetType(string) requires the Assembly qualified name, unless it's within mscorlib.dll

    Because your project could be using a myriad of dlls it can be tough to get the right one that has the actual type. One way I do it is that if it's your class, you can use System.Reflection.Assembly.GetExecutingAssembly().GetType("MyType");, otherwise you can use a known type to get a specific assembly without having to screw around with the qualified assembly name stuff.
    i.e.
    System.Reflection.Assembly.GetAssembly(typeof(GameObject)/*My known type*/).GetType("UnityEngine.Transform");
     
    jesper42 likes this.
  5. TakuanDaikon

    TakuanDaikon

    Joined:
    Jun 6, 2010
    Posts:
    268
    I also encountered this issue while working on some custom Asset Editor code that needs to serialize and deserialize Asset data, and discovered that Type.GetType() will return the expected Type for some types, like those defined in the Mono Runtime, while returning NULL for those defined in UnityEngine (for instance).

    Because of this, I decided to create a wrapper function that seems to work for all of the types I've tried it on so far:

    Code (csharp):
    1. public static Type GetType( string TypeName )
    2. {
    3.  
    4.     // Try Type.GetType() first. This will work with types defined
    5.     // by the Mono runtime, etc.
    6.     var type = Type.GetType( TypeName );
    7.  
    8.     // If it worked, then we're done here
    9.     if( type != null )
    10.         return type;
    11.  
    12.     // Get the name of the assembly (Assumption is that we are using
    13.     // fully-qualified type names)
    14.     var assemblyName = TypeName.Substring( 0, TypeName.IndexOf( '.' ) );
    15.  
    16.     // Attempt to load the indicated Assembly
    17.     var assembly = Assembly.LoadWithPartialName( assemblyName );
    18.     if( assembly == null )
    19.         return null;
    20.  
    21.     // Ask that assembly to return the proper Type
    22.     return assembly.GetType( TypeName );
    23.  
    24. }
    I hope this is helpful to someone.
     
    oAzuehT likes this.
  6. Bryarey

    Bryarey

    Joined:
    Aug 24, 2014
    Posts:
    25
    Cool, it was helpful (i am have to change <Assembly.LoadWithPartialName> to <System.Reflection.Assembly.LoadWithPartialName>, and all works fine), but now i got an error <FieldAccessException: Cannot set a constant field> at next code:

    Code (csharp):
    1. var newObj=System.Activator.CreateInstance(type);
    2.               var fields=type.GetFields();
    3.        for (var i=0; i<fields.Length; i++)
    4.          {
    5.          switch(fields.FieldType)
    6.            {
    7.            case String:
    8.            fields.SetValue(newObj,GetString(fields.Name));
    9.            break;
    10.            case int:
    11.            fields.SetValue(newObj,GetInt(fields.Name));
    12.            break;
    13.            case float:
    14.            fields.SetValue(newObj,GetFloat(fields.Name)); ////THERE is error, because i try to set float variable
    15.            break;
    16.            case boolean:
    17.            fields.SetValue(newObj,GetBoolean(fields.Name));
    18.            break;
    19.            case Object:
    20.            fields.SetValue(newObj,GetObject(fields.Name));
    21.            break;
    22.            default:
    23.            if (fields.FieldType instanceof Object)
    24.              {
    25.              fields.SetValue(newObj,GetObject(fields[i].Name));
    26.              }
    27.            else
    28.              {
    29.              fields[i].SetValue(newObj,GetString(fields[i].Name));
    30.              }
    31.            break;
    32.            }
    33.          }
    What does it means and what i have to do with that?
     
  7. csotomon

    csotomon

    Joined:
    Apr 28, 2012
    Posts:
    20
    Try
    Code (CSharp):
    1. System.Type test= Types.GetType("UnityEngine.BoxCollider2D","UnityEngine");