Nodachisoft Nodachisoft logo, Katana Sword Icon
  
@Amaji✎ Update January -11-2022

System.Type.GetType returns null in Unity C# code

System.Type.GetType returns null in Unity C# code

Summury Of Article

When programming in Unity in C#(.NET), I've been using System.Type.GetType in the test code or from another library, but it returns null. GetType from Here are the causes and solutions.

My Environment

  • Unity2020.3 LTS
  • Windows10 HomeEdition

Cause and solution

In my case, this problem occurred when I split a common part of one project into different DLLs.

GetType Specification

System.Type.GetType retrieves the specified Type from within its own assembly instance that is running when used as follows.

 
GetType
System.Type wolfType = System.Type.GetType("MyGame.Enemy.Wolf");

To search within your own assembly instance, you can use If you run it from within Unity's test code (Unity Test Runner) or code created under another library, it will apply a different assembly definition file and the GetType does not work. (In my environment, it didn't work even if the reference was written properly in the Unity assembly definition file (asmdef file).

Referencing the other assembly

In such cases, when you use GetType, specify which dll you are referring to.

For example, if you want to access the class MyGame.Enemy.Wolf in MyGame.dll from a test code or library that applies a different assembly definition file, you can write:

 
GetType
System.Type wolfType = System.Type.GetType("MyGame.Enemy.Wolf,MyGame");

The way to write it is as follows.

 
GetType
System.Type.GetType("Path,DLL name");

Extra Info

With the following code, you can get the name of your own assembly fullname that you are running and a list of the Type that the assembly exposes to the outside.

 
DispAssembly.cs
Assembly asm = Assembly.GetExecutingAssembly();
UnityEngine.Debug.Log("Assembly name=" + asm.FullName);
Log("Assembly name = " + asm.FullName); foreach (System.Type type in asm.GetExportedTypes()) {
    UnityEngine.Debug.Log(type);
}

I use UnityEngine.Debug.Log(type) to output to Unity's debugger, but if you want to output to a standard console, rewrite it to Console.WriteLine(type) and run it.

That's all!

Thanks for reading!

Websites and references that were very helpful.

Page History

date modification
none
 
 
Message sent

Thank you for your message.

Something error has occured!

Sorry. The Error has occurred.We apologize for the inconvenience.Please try again in a few minutes or contact us via DM below.

Twitter:@NodachiSoft_eng
Name:
 
Replay To:
 
Message:
 
Back
Check the content!

Send the following information to us. If you are happy with your submission, please click "Send". If you want to modify it, please click "Back".

Name:
 
Reply To:
 
Message:
 
Enter a confirmation key to make sure that you are not operating from a Robot.
Confirmation Key is 95
Back
 / 
Go to Confirmation
Entry fields
Go to Confirmation

There are 5 articles that may be relevant!

How to get parent and child GameObjects with relative paths in Unity

How to get parent and child GameObjects with relative paths in Unity

#Unity✎ 2022-1-11
Getting parent and child GameObjects with relative paths in Unity. How to get the parent's children, etc. by a retroactive path.
Table Of Contents
System.Type.GetType returns null in Unity C# code
System.Type.GetType returns null in Unity C# code
Summury Of Article
Summury Of Article
My Environment
My Environment
Cause and solution
Cause and solution
GetType Specification
GetType Specification
Referencing the other assembly
Referencing the other assembly
Extra Info
Extra Info
Websites and references that were very helpful.
Websites and references that were very helpful.
Page History
Page History
Nodachisoft © 2021