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

Unity/Mono bug exposed - How to break Unity in under 20 lines of code

Discussion in 'Scripting' started by Zergling103, Mar 23, 2017.

  1. Zergling103

    Zergling103

    Joined:
    Aug 16, 2011
    Posts:
    392
    I found an obscure bug in Unity's version of Mono, where if you paste the following code anywhere in your project, anything that uses System.Reflection.Assembly.GetTypes() will throw a ReflectionTypeLoadException. This includes Unity itself, as Unity relies on Reflection to update the inspector.

    Code (csharp):
    1. public interface IInterface
    2. {
    3.     void Method<TMethodArg>();
    4. }      
    5.    
    6. public abstract class ClassA : IInterface
    7. {
    8.     public abstract void Method<TMethodArg>();
    9. }    
    10.                                                              
    11. public abstract class ClassB<TClassArg> : ClassA { }
    12.  
    13. public class ClassC : ClassB<object>
    14. {
    15.     public override void Method<TMethodArg>() { }
    16. }
    Note that you don't even have to call, instantiate or otherwise touch the above lines of code in order for the bug to happen - just having it in your project is sufficient.

    Obviously the above code doesn't make a lot of sense, but this is the minimal amount of complexity required to reproduce the bug. In other words, I tried to eliminate as many elements from the code as I could while still causing the bug to happen.

    Apparently this problem is specific to Unity, and doesn't appear to happen in other C# environments.

    Interesting find? I'm submitting a bug report for it now. :)
     
    Last edited: Mar 23, 2017
    GarthSmith likes this.
  2. Zergling103

    Zergling103

    Joined:
    Aug 16, 2011
    Posts:
    392
  3. jackishere

    jackishere

    Joined:
    Mar 1, 2014
    Posts:
    22
    Hi this is indeed interesting find, I have tried it and i can repro it.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    Check if it happens in any of the updated betas available here.

    If it's fixed there, then the fix is in the pipeline. If it's not, write a post about it in that forum, as the developers working on the Mono upgrade will want to know about things like this.

    In all cases where I have seen things like this happening, it's bugs in Unity's old Mono version that has already been fixed. I've got quite a few bugs in this vein that were returned as "postponed until Mono upgrade". As an example, I sent a report for the bug in this thread, and it was postponed. I also checked, and it works in the mono upgrade beta.
     
    lordofduct likes this.
  5. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    I don't even want to think about the birds nest you had to dig through to narrow this down.
     
    Zergling103 likes this.
  6. Zergling103

    Zergling103

    Joined:
    Aug 16, 2011
    Posts:
    392
    Unforunately it would be unwise for me to allocate much more time towards investigating this bug. However for someone who does have the necessary version of Unity installed, it would take less than a minute to reproduce, so there can't be too much harm in sharing it there anyway.
     
  7. Zergling103

    Zergling103

    Joined:
    Aug 16, 2011
    Posts:
    392
    Luckily, I didn't make too many changes before noticing the bug. It took about half a day, but it could have been far worse.