// Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray.origin, ray.direction, out hit, Mathf.Infinity, -1, QueryTriggerInteraction.Ignore)) { if (targetable != null) { targetable.ClearTarget(); } targetable = hit.collider.gameObject.transform.root.GetComponent <Targetable>(); if (targetable != null) { targetable.SetTarget(); Attack attack = GetComponent <Attack>(); if (attack != null) { Attackable attackable = targetable.GetComponent <Attackable>(); if (attackable != null) { //this object can attack, and the target is attackable attack.target = targetable.gameObject; } } } } } }