Apologies in advance if it’s a bit long winded :
I’m trying to create a component which allows me to select any object in my world.
I’m not that experienced with Unity3D and tried do this first with sending messages. The problem I walked into is that I need to ensure that certain objects can be selected and have the same function calls. So I went with Interface classes. I want this game component to work globally.
For example, I have units that can move. But the moment I select them I need to show their walk range. ( As of right now this simply works )
This “Behavior” is different from, let’s say, a crate you can click on to see any information on. Or an enemy for that matter ( like showing health/ATK ).
However I want them to have a common structure rather than throwing everything into different components. And have to ask whether GameObject A has Selection Component A and if not does he have selection Component B. Etc etc.
I have the feeling this can be solved by messages. But I simply can’t figure it out.
So I ask: How is selection for game objects usually handled and is the method I’m using completely alien to the intention of Unity’s framework guidelines( see code snippets below)?
This is the “Selection” Component.
To identify what object can be selected I’ve created a ISelectable interface:
public interface ISelectable {
void selected();
void unselect();
bool selectable{get;set;}
}
This way gameobjects can handle their selection routine in their own components.
So in my selection component I do this ( note this isn’t all the code):
if(firstSelectedObj == null)
{
ISelectable sObj = (ISelectable)hit.collider.gameObject.GetComponent(typeof(ISelectable));
if(sObj != null){
if(sObj.selectable){
firstSelectedObj = hit.collider.gameObject;
sObj.selected();
}
} else return;
}
So basically I check whether the object my ray hits is even relevant by checking if the getComponent method doesn’t return a null object.
Because a second object can be selected In combination with the first I’ve also added an interface so that the second selected gameobject can be passed down.
else if(secondSelectedObj == null)
{
ISelectable firsStObj = (ISelectable)firstSelectedObj.GetComponent(typeof(ISelectable));
ISelectable secondSObj = (ISelectable)hit.collider.gameObject.GetComponent(typeof(ISelectable));
secondSelectedObj = hit.collider.gameObject;
IGObjectReceiver rObj = (IGObjectReceiver)firstSelectedObj.GetComponent(typeof(IGObjectReceiver));
if(rObj != null)
{
//Send second selection to first
rObj.receive(secondSelectedObj);
}
firstSelectedObj = null;
secondSelectedObj = null;
if(firsStObj != null){
//if(selectedObj.can)
firsStObj.unselect();
}
if(secondSObj != null){
secondSObj.unselect();
}
}
So by clicking on an object the select() method is called. Which handles stuff that needs to happen on a click. In this case it concerns a unit that needs to move from tile to tile. So in the receive(GameObject obj) the Tile is passed and checked by tag if it’s indeed a tile object we need information from.