I'm trying to damage my enemy via a raycast, but its not working.

So, I have an ability system that can deal damage to things via a raycast. While it works with enemies I've made separately from the Opsive kit, I can't get it to interact with Opsive's health components.

My code is as followed:

"EnemyHealth = Enemy.GetComponent<CharacterHealth>();"

And I get this error from using it:

"Cannot implicitly convert type 'Opsive.UltimateCharacterController.Traits.CharacterHealth' to 'int'"

I've tried other ways to do this, but no luck.
 
I'm guessing that EnemyHealth is an int? You can get the health value from CharacterHealth.HealthValue.

Code:
float health = Enemy.GetComponent<CharacterHealth>().HealthValue;
 
I'm guessing that EnemyHealth is an int? You can get the health value from CharacterHealth.HealthValue.

Code:
float health = Enemy.GetComponent<CharacterHealth>().HealthValue;

Hello! I know I marked this thread as solved, but I'm getting a strange issue. I got the health from my enemy, and I see the script knows my enemy's current health, but the enemy doesn't seem to notice that its being hurt by the raycast. When I shoot it with an opsive weapon, the health goes down as normal, but when the ray hits it, the health numbers go down but the enemy doesn't actually take the damage.

My raycast is hitting the enemy's capsule collider, if that's important. Also, I can open a new thread if needed.
 
If the capsule collider is on a separate object to the Health component, then GetComponent<CharacterHealth>() on the capsule collider that you hit won't get the component. You should try hitCollider.transform.GetComponentInParent<CharacterHealth>() instead: https://docs.unity3d.com/ScriptReference/Component.GetComponentInParent.html
Hmm, I did this, but now I get this error:

Cannot implicitly convert type 'Opsive.UltimateCharacterController.Traits.CharacterHealth' to 'float'

Which is strange because the code was able to read the health value before. Maybe I should find an alternate method of applying damage to the enemy?
 
If you're trying to get the HealthValue then you'll need to use hitCollider.transform.GetComponentInParent<CharacterHealth>().HealthValue, however you can't modify the HealthValue directly, so you can instead use the Damage method, i.e.:

C#:
var health = hitCollider.transform.GetComponentInParent<Health>();
health.Damage(10);
 
If you're trying to get the HealthValue then you'll need to use hitCollider.transform.GetComponentInParent<CharacterHealth>().HealthValue, however you can't modify the HealthValue directly, so you can instead use the Damage method, i.e.:

C#:
var health = hitCollider.transform.GetComponentInParent<Health>();
health.Damage(10);

Oh yeah. I remember learning this earlier, but I guess I forgot again. I think you were the one that responded before, too, so I apologize for essentially asking the same thing!

Thankfully, everything works now. I'll commit this to my memory from now on!
 
Top