Hello everyone, can I just get a santity check. I'm building a simple puzzle game where game objects (meshes) are getting clicked. I currently have this working fine by including a empty game object in the scene that has a script attached to it. This script, on update, has the following code
private void Update()
{
if (Mouse.current.leftButton.wasPressedThisFrame)
{
if (loadingCubesCamera == null) return;
Ray ray = loadingCubesCamera.ScreenPointToRay(Mouse.current.position.ReadValue());
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
InterferenceClickCube cubeClick = hit.collider.GetComponent<InterferenceClickCube>();
if (cubeClick != null)
{
cubeClick.OnClicked();
}
else
{
InterferenceClickFile fileClick = hit.collider.GetComponent<InterferenceClickFile>();
if (fileClick != null)
{
fileClick.OnClicked();
}
}
}
}
}
Each game object that is clickable has a script attached to it (InterferenceClickCube, InterferenceClickFile) in the above example. I'm worried that as I add more clickable item types on the scene this approach is going to get messy. I'm not aware of any other approaches to doing this. Are there any alternatives? I'm open to any suggestions/comments and I'm happy to research if you could provide search terms to use.