Compare gameobjects

127 views
Skip to first unread message

Bart Wttewaall

unread,
Dec 20, 2010, 5:40:15 AM12/20/10
to unity3d-d...@googlegroups.com
Hi,

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,
Bart

Joaquin Grech

unread,
Dec 20, 2010, 6:08:55 AM12/20/10
to unity3d-d...@googlegroups.com
Bart,
 
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
Joaquin Grech Gomendio

International MBA, Concentration in Finance & Entrepreneurship

IE Business School, President IE Spain Club, www.iespainclub.com




--
You received this message because you are subscribed to the Google
Groups "Unity3D Developers" group.
To post to this group, send email to unity3d-d...@googlegroups.com
To unsubscribe from this group, send email to
unity3d-develop...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/unity3d-developers?hl=en

Bart Wttewaall

unread,
Dec 20, 2010, 7:12:40 AM12/20/10
to unity3d-d...@googlegroups.com
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 <joaqui...@gmail.com>
Reply all
Reply to author
Forward
0 new messages