I used a TryGetComponent here and it functions the same. Is this an acceptable use case for this scenario?
Yeah that seems like a good use case, the only thing I would change personally is check for the mouse input first before TryGetComponent
Any specific reason why you’d do that?
The only reason I would do that is GetMouse is being checked for every target in the for loop so if you check for the mouse first you don’t even have to enter the for loop at all or check for components. It’s just a small optimization
Thank you for the explanation. Helps with a sense of understanding optimization
That will cause an issue later when we introduce affordances (cursor changing with the type of action available at that location).
Sorry about that, didn’t realize the context
It doesn’t come in till the last section of the course.
Yes, TryGetComponent is a powerful tool which, unfortunately, wasn’t a thing in the version we recorded this course in.
Nifty tidbit, you can omit the <CombatTarget>
portion of the method call, because the compiler will properly infer the type from the out parameter
if(hit.transform.TryGetComponent(out CombatTarget target))
Also remember when using this that target is guaranteed to be valid within the if code block, but no such guarantee exists once you are out of the code block.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.