So basically im using a ray to find what im mousing over and attaching it when i click on the unit. so unit selection.
once i have done that im trying to get right click to issue a move command and on the unit i have a function to move them.
As this will be accessing different scripts depending on what unit i select im having difficulty with attaching the script through code to use its functions. any idea id really appreciate it.
Mouse Manager Script
if (Input.GetKey(KeyCode.Mouse0))
{
if (highlightedObject.layer == 9)
{
ClearSelected();
}
else if (highlightedObject.layer == 8)
{
selectedObj = highlightedObject;
}
print("highlightedObject.layer Unknown");
}
if (Input.GetKey(KeyCode.Mouse1))
{
if (selectedObj == null)
{
return;
}
selectedObjName = selectedObj.name;
//Unit = selectedObj.GetComponent<selectedObjName>();
//Unit.Move();
//nothings working
print(selectedObjName + " how do i move?");
}
Cube script (Not made a proper model yet)
public class Cube : MonoBehaviour {
public void Move()
{
print("Cube move called");
}
// public class Move
// {
// void MoveAction()
// {
// print("Cube move called");
// }
// }
}
Hey there. If you’re saying you’ll have different scripts but they’ll share the ability to “Move”, you have a couple of options.
1 is to create a base class with any/all methods that will be shared by every unit. Then, you can create child classes but look for the base class when you’re clicking, and use its move method.
the other would be to use interfaces. Like “IMoveable” or something like that, and you can also look for the interface when you’re doing the Unit selection.
Are you familiar with that? Am I understanding your question correctly?
One other thing, is that you could use the IPointer interfaces for unit (mouse) targetting. These interfaces can intercept mouse enter/exit/down/up/click etc – and you use them on scripts that are on the units.