Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

System.Type.GetType does not work in release build.

Discussion in 'Scripting' started by deck57, Jul 22, 2022.

  1. deck57

    deck57

    Joined:
    Aug 24, 2017
    Posts:
    11
    We recently upgraded our game from Unity 2019 LTS to 2021 LTS and came across a bug that only occurs in our game build (IL2CPP), not in Unity editor.

    I have the following code structure as example:
    Code (CSharp):
    1. public class Character {
    2.     public enum MovementType {
    3.     }
    4. }
    5.  
    6. public class CrimsonDervish_Toreign_CombatMode {
    7.     public class FinalFight {
    8.         public class Character {
    9.         }
    10.     }
    11. }
    12.  
    The problem is System.Type.GetType("Character") will return any Nested class named Character instead of global::Character.

    Examples:
    Code (CSharp):
    1.  
    2. // This returns "Character, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" which is correct
    3. string characterClassTypeName = typeof(global::Character).AssemblyQualifiedName;
    4.  
    5. // This will return the type CrimsonDervish_Toreign_CombatMode.FinalFight.Character and NOT global::Character like I would expect
    6. System.Type.GetType(characterClassTypeName);
    7. // Same with this
    8. System.Type.GetType("Character");
    9. // This returns null instead of the nested type Character.MovementType
    10. System.Type.GetType("Character+MovementType");
    11. // Same with this
    12. System.Type.GetType(typeof(Character.MovementType).AssemblyQualifiedName);
    This issue does not occur in unity editor or in 2019 TLS (build and editor), only in 2021 TLS IL2CPP build.
    What is the correct way to get the type global::Character from a string?
     
  2. deck57

    deck57

    Joined:
    Aug 24, 2017
    Posts:
    11
    After more tests I am pretty confident this is a new IL2CPP bug.
     
  3. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
  4. deck57

    deck57

    Joined:
    Aug 24, 2017
    Posts:
    11
    I did more search and it seems this bug has already been reported here and fixed in 2021.3.7. I will test again when this version comes out.
     
  5. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    I'm glad you found that. I even reviewed the fix for that bug and had forgotten about it!
     
  6. deck57

    deck57

    Joined:
    Aug 24, 2017
    Posts:
    11
    Glad it's already fixed, thank you Unity team!
     
  7. Alvarden

    Alvarden

    Joined:
    Feb 22, 2019
    Posts:
    55
    Hello there. On Unity 2021.3.23, this issue still persist, at least in my case.
     
  8. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,771
    I wonder if there is some subtle difference in the case you have which was not corrected. Can you provide more details about your case or submit a bug report?