Getting CombatTarget from hit.collider

Hello everybody,

during this chapter I browsing a lot the Unity documentation and it leads me to write something like this :

 if (hit.collider.TryGetComponent(out CombatTarget target)) {
     if (Input.GetMouseButtonDown(0)) {
         GetComponent<Fighter>().Attack(target);
     }
}

What do you think about that ?

It’s seems to be more or less the same thing that the transform.GetComponent do and I used to use the transform.GetComponent way but I found this solution “cute” and clean.

Let me know :wink:

Regards !

That route would certainly work.
Be aware that we are going to put the entire component handling system over on it’s head by the end of the course, and this beautiful fragment of code will become obsolete, but for now this is good, and I strongly encourage experimenting and studying the Unity documentation.

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms