Search Unity

objects derive from monobehaviour is equal

Discussion in 'Scripting' started by GlorySimba, Jul 19, 2013.

  1. GlorySimba

    GlorySimba

    Joined:
    Apr 18, 2013
    Posts:
    10
    hi all!

    i have a question:
    assume there is a script derived from monobehaviour, and i init it to different objects, such as A, B, but the props of them are not the same. and i test whether A == B, found that A always equals to B.

    in my mind, different objects are not equal to another, but in unity, as long as it derived form monobehaviour, they are equals. what's going on in the internal of monobehaviour?
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,619
    I'm pretty sure that's not the case. I've checked equality on MonoBehaviour references before and it behaves exactly as it should - returning true if the references are to the same object, otherwise false.

    Code please?
     
  3. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
    Seems to work fine to me. Boo test below.

    Code (csharp):
    1. import UnityEngine
    2.  
    3. class MakeObjects (MonoBehaviour):
    4.     def Start():
    5.         person = gameObject.AddComponent(Person)
    6.         animal = gameObject.AddComponent(Animal)
    7.         if person == animal:
    8.             Debug.Log("${person.GetType()} and ${animal.GetType()} are the same.")
    9.         else:
    10.             Debug.Log("${person.GetType()} and ${animal.GetType()} are different.")
    11.                    
    12. class Animal (MonoBehaviour):
    13.     Name as string
    14.    
    15. class Person (MonoBehaviour):
    16.     Name as string
    17.