I am trying to compare two GameObjects to see if they are the same:
This doesn't work, because since I am instantiating the GameObjects at runtime, bar might be called "GameObject (Clone)" and == seems to only compare the name value of the GameObjects. Short of manually setting the name of the GameObject, are there any easier ways to do this?
asked Aug 10 '11 at 05:38 PM
Hi I'm not sure what your exactly but your code should work if you want to compare to see if the objects are the same instance. If you want to check to see if they are of the same type(from the same prefab) try use Object.GetType to compare the type of the object. I'm not sure if it will work because I'm I don't fully understand how prefabs work but give it a try.
Edit: You say that the when comparing the GameObjects it compares by name. Is this true? So it doesn't compare them by reference?
Edit2: I just tested the above code and it doesn't work. GetType just returns GameObject. If comparing 2 GameObjects does compare by name and you want to see if they are the same instance you could use the instanceID to check if they are the same.