• 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 t$$anonymous$$s?

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(MyType)

?

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 Kiernan: 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 t$$anonymous$$s:

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
13

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

I also encountered t$$anonymous$$s issue w$$anonymous$$le 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, w$$anonymous$$le returning NULL for those defined in UnityEngine (for instance).

Because of t$$anonymous$$s, 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. T$$anonymous$$s 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 t$$anonymous$$s 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 instead ;)

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Name GUI input help in C# 0 Answers

C# Generic return type 1 Answer

how can i use ZipArchive class ,How can i use .net ZipArchive Class 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges