Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

CompareBaseObjectsInternal can only be called from the main thread == annoying

Discussion in 'Scripting' started by Zergling103, Aug 14, 2013.

  1. Zergling103


    Aug 16, 2011
    Hi there,

    It seems that comparing two objects using ==, an atomic operation, causes Unity to moan if you do it outside the main thread.

    This is a completely thread safe operation, but someone thought it was a good idea to mark it otherwise.

    Is there some way to bypass this? Perhaps UnityEngine.Object has an overload for == and reinterpreting it as System.Object would use a plain ol' comparison with no spooky thread checks.

    I've considered making an empty, read-only dummy object, comparer, as a field for one of my components. Checking if two instances are the same would just be a matter of comparing their comparers.

    However, this prevents you from using it as a dictionary key, if you need to access some fields of the component. So I made a custom object for a comparer that is initialized with a reference to the component that it belongs to. However this produces circular references, which is bad practice because it confuses the garbage collector.

    The last two solutions produce messier, harder to maintain code.

    Any other ideas? I'd love to know, thanks!

    P.S.: Dear Unity dev team: Please mark == between two UnityEngine.Object's as thread safe or do not overload it. Thank you.
  2. Loius


    Aug 16, 2012
    if ( ReferenceEquals(null,target) )

    is the same as

    if ( null == target )

    but it's threadsafe. It does only check references, doesn't do any of Unity's overloaded stuff.
    Maisey likes this.