Compare gameobjects

Skip to first unread message

Bart Wttewaall

Dec 20, 2010, 5:40:15 AM12/20/10

I'm looking for a way to compare two GameObjects on their Type.

public var chair1:GameObject;
public var chair2:GameObject;
public var chair3:GameObject;
public var chair4:GameObject;
public var table:GameObject;

public var selectedObject:GameObject; // either a chair or table

This won't work:
print(selectedObject instanceof chair1);

Neither does this:
print(selectedObject.GetType() == chair1.GetType())
It returns "GameObject", so that's of no use.

I'd lke to do something like this:
if (selectedObject is chairPrefab) sit();
else if (selectedObject is tablePrefab) dinnerIsServed();

So how do I compare selectedObject to some other GameObject on their Type or prefab class instead of reference?

With regards,

Joaquin Grech

Dec 20, 2010, 6:08:55 AM12/20/10
those are not types, they are just variable names. The type is always "GameObject".
So if you want to know if 2 variables are equal then if (selectedObject == chair1) ...
In any case, I would begin using C# because it's clearer for all these things ;)
Joaquin Grech Gomendio

International MBA, Concentration in Finance & Entrepreneurship

IE Business School, President IE Spain Club,

You received this message because you are subscribed to the Google
Groups "Unity3D Developers" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

Bart Wttewaall

Dec 20, 2010, 7:12:40 AM12/20/10
Yes I know, that example was pseudo code (whishfull thinking) ;)
In my real project I have 300 clickable books, so checking if each one is the selectedObject is not very smart.

I've solved it this way:
On creation I add a value object as a component on each clickable/selectable object.
Now I can check on type, isClickable and other properties.

Is this the best way or does someone have a better solution?

Cheers, Bart

2010/12/20 Joaquin Grech <>
Reply all
Reply to author
0 new messages