Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

System.Type.GetType("UnityEngine.Rigidbody"); returning NULL?

Discussion in 'Scripting' started by SpookyCat, Jan 28, 2022.

  1. SpookyCat

    SpookyCat

    Joined:
    Jan 25, 2010
    Posts:
    3,748
    I am trying to use Reflection to get Types by names using Type.GetType, and it works fine for my own scripts, finds them all, but it does not find any Unity types. I have tried numerous formats for the names but they all return NULL, such as:
    Code (CSharp):
    1.     System.Type rval = System.Type.GetType("UnityEngine.Rigidbody");
    2.     System.Type rval = System.Type.GetType("UnityEngine.Rigidbody, UnityEngine");
    3.     System.Type rval = System.Type.GetType("UnityEngine.Rigidbody, Assembly-CSharp");
    4.  
    Am I missing something? This used to work several versions of Unity ago, so something has changed, anyone have any ideas to get this work again?
     
  2. Try the
    UnityEngine.PhysicsModule
    assembly.

    Edit:
    Although this also works for me:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Test : MonoBehaviour
    4. {
    5.     private void Start() => Debug.Log(System.Type.GetType("UnityEngine.Rigidbody, UnityEngine"));
    6. }
    screenshot.png
     
    Last edited by a moderator: Jan 28, 2022
    Bunny83 likes this.
  3. SpookyCat

    SpookyCat

    Joined:
    Jan 25, 2010
    Posts:
    3,748
    Well seems using Type.GetType(string name, bool throw, bool ignorecase) with ignorecase set to true doesn't work.
    So this line returns the type:
    Code (CSharp):
    1. System.Type type = System.Type.GetType("UnityEngine.Rigidbody, UnityEngine", false, false);
    But this returns NULL:
    Code (CSharp):
    1. System.Type type = System.Type.GetType("UnityEngine.Rigidbody, UnityEngine", false, true);
    But it works fine with non Unity types.
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,915
    Might be related to this.

    You usually never want to do an case insensitive type lookup anyways. Even though it's not recommended, one can create a type called "MyClass" and also one called "myclass" and "Myclass" and "myClass". Those would be all valid type names and each represents a different type. I'm not even sure why there's even an case insensitive lookup in the first place ^^.
     
  5. won-gyu

    won-gyu

    Joined:
    Mar 23, 2018
    Posts:
    33
    try
    Code (CSharp):
    1. Type.GetType($"{classname},Assembly-CSharp");