• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by CommitteeWind · Jan 17, 2012 at 03:58 AM · c#.nettype

Type.GetType(string) does not work in Unity

Calling:

 Type type = Type.GetType("MyType");
 Debug.Log("typeIsNull = " + (type == null));

Will result in "typeIsNull = true", and yes the public class MyType exists in my unity project.

How do I fix this?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Jan 17, 2012 at 04:09 AM 1
Share

Why not just use

 typeof($$anonymous$$yType)

?

Doesn't that do what you want?

avatar image CommitteeWind · Jan 17, 2012 at 04:16 AM 1
Share

it would but i dont actually know the type at run time. i was just emulating that it will come to me in the form of a string.

avatar image slippdouglas · Mar 25, 2014 at 06:29 AM 0
Share

@Storm $$anonymous$$iernan: I think you meant “but i dont actually know the type at compile time.”

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by jahroy · Jan 17, 2012 at 05:05 AM

In java you would use the method Class.forName().

So... I googled c# class.forname equivalent and found this:

http://msdn.microsoft.com/en-us/library/aa310400%28v=vs.71%29.aspx

Hope it helps.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
12

Answer by TakuanDaikon · Apr 12, 2012 at 03:15 PM

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, in the same assembly as the caller, etc.
     var type = Type.GetType( TypeName );
 
     // If it worked, then we're done here
     if( type != null )
         return type;
 
     // If the TypeName is a full name, then we can try loading the defining assembly directly
     if( TypeName.Contains( "." ) )
     {
 
         // 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.Load( assemblyName );
         if( assembly == null )
             return null;
 
         // Ask that assembly to return the proper Type
         type = assembly.GetType( TypeName );
         if( type != null )
             return type;
 
     }
 
     // If we still haven't found the proper type, we can enumerate all of the 
     // loaded assemblies and see if any of them define the type
     var currentAssembly = Assembly.GetExecutingAssembly();
     var referencedAssemblies = currentAssembly.GetReferencedAssemblies();
     foreach( var assemblyName in referencedAssemblies )
     {
 
         // Load the referenced assembly
         var assembly = Assembly.Load( assemblyName );
         if( assembly != null )
         {
             // See if that assembly defines the named type
             type = assembly.GetType( TypeName );
             if( type != null )
                 return type;
         }
     }
 
     // The type just couldn't be found...
     return null;
 
 }


I hope this is helpful to someone.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TakuanDaikon · Apr 12, 2012 at 04:17 PM 0
Share

For what it's worth, I disovered after posting the code above that Assembly.LoadWithPartialName() is deprecated, but Assembly.Load() does indeed work.

avatar image Bunny83 · Apr 13, 2012 at 10:02 PM 2
Share

Just want to add that using Assembly.Load isn't really the way to go. It works, but not in the webplayer because you are not allowed to load foreign assemblies by name.

To get all loaded assemblies use System.AppDomain.CurrentDomain.GetAssemblies() which returns an array of Assembly. GetAssemblies works also in the webplayer.

I've just posted a function on this question that returns a list of all types that are derived from a certain type. It can easily be modified to search for a certain type name ins$$anonymous$$d ;)

avatar image mikaelK Bunny83 · Sep 19, 2020 at 03:15 PM 0
Share

@Bunny83 Big thanks! You just saved me a lot of work. This is the only one that worked for me on standalone for some reason.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Error CS0425: Works in DLL but not in Unity 0 Answers

Vector3 is a 'type' but it is used like an 'variable' (C#) 1 Answer

How to store coroutines inside a List? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges