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

Bug Entity Equals method doesn't manage object or null value

Discussion in 'Entity Component System' started by Sylmerria, Jan 7, 2021.

  1. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    369
    Hi,

    before submitting a bug, can you confirm me that it is indeed a behavior not intended :

    The override Entity Equals methods :
    Code (CSharp):
    1.         /// <summary>
    2.         /// Entity instances are equal if they refer to the same entity.
    3.         /// </summary>
    4.         /// <param name="compare">The object to compare to this Entity.</param>
    5.         /// <returns>True, if the compare parameter contains an Entity object having the same Index and Version
    6.         /// as this Entity.</returns>
    7.         public override bool Equals(object compare)
    8.         {
    9.             return this == (Entity)compare;
    10.         }

    This method which have to manage managed object raise exception with all object which are not an entity.

    This method for exemple raise an "System.InvalidCastException : Specified cast is not valid."

    Code (CSharp):
    1.         [Test]
    2.         public void EntityEqualsTest()
    3.         {
    4.             var e = new Entity();
    5.             var o = new object();
    6.             Debug.Log(e.Equals(o));
    7.         }

    A possible solution :

    Code (CSharp):
    1.  
    2. /// <summary>
    3. /// Entity instances are equal if they refer to the same entity.
    4. /// </summary>
    5. /// <param name="compare">The object to compare to this Entity.</param>
    6. /// <returns>True, if the compare parameter contains an Entity object having the same Index and Version
    7. /// as this Entity.</returns>
    8. public override bool Equals(object compare)
    9. {
    10.     return compare is Entity entity && this == entity;
    11. }
    12.  


    Edit: report done, Case 1304591
     
    Last edited: Jan 8, 2021