I’m making a turn based strategy game and I want to toggle a field in a script for a unit from another script that registers a mouseclick, but I don’t know how to check which unit was clicked. Here is the script:
public class Clicked : MonoBehaviour
{
[SerializeField] private LayerMask unitLayer;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit rayHit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out rayHit, Mathf.Infinity, unitLayer))
{
T34 selectedOne = rayHit.collider.GetComponent<T34>();
selectedOne.selected = !selectedOne.selected;
}
}
}
}
As you can see “T34” is my placeholder currently, but it should be whichever unit was selected. It seems that T34 selectedOne = rayHit.collider.GetComponent<T34>();
is the bit that needs changing but otherwise I have no clue.
Hope my question makes sense.