Hi dudes, I got new problem
I’m making Unity instances from XML script.
And code below returns null.
Type type = System.Type.GetType("Transform");
I also tried code below
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,
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?
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”);
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:
public static Type GetType( string TypeName )
{
// Try Type.GetType() first. This will work with types defined
// by the Mono runtime, etc.
var type = Type.GetType( TypeName );
// If it worked, then we're done here
if( type != null )
return type;
// Get the name of the assembly (Assumption is that we are using
// fully-qualified type names)
var assemblyName = TypeName.Substring( 0, TypeName.IndexOf( '.' ) );
// Attempt to load the indicated Assembly
var assembly = Assembly.LoadWithPartialName( assemblyName );
if( assembly == null )
return null;
// Ask that assembly to return the proper Type
return assembly.GetType( TypeName );
}
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:
var newObj=System.Activator.CreateInstance(type);
var fields=type.GetFields();
for (var i=0; i<fields.Length; i++)
{
switch(fields.FieldType)
{
case String:
fields.SetValue(newObj,GetString(fields.Name));
break;
case int:
fields.SetValue(newObj,GetInt(fields.Name));
break;
case float:
fields.SetValue(newObj,GetFloat(fields.Name)); ////THERE is error, because i try to set float variable
break;
case boolean:
fields.SetValue(newObj,GetBoolean(fields.Name));
break;
case Object:
fields.SetValue(newObj,GetObject(fields.Name));
break;
default:
if (fields.FieldType instanceof Object)
{
fields.SetValue(newObj,GetObject(fields[i].Name));
}
else
{
fields[i].SetValue(newObj,GetString(fields[i].Name));
}
break;
}
}
What does it means and what i have to do with that?