Problem with raycast, and if hit enemy.

if(hit != null) {

			enemyHit = hit.transform.tag == "Enemy";
			comboCount += 1;
			
			if(enemyHit){
				enemyHit.gameObject.GetComponent(Health).curHealth -= damage;
			}
		   	
		 }

The title explains. Can anyone help? I get an missingfieldexception. I’m noob at programming raycast…

Thanks :smiley:

I believe line 7 should be:

hit.transform.GetComponent(Health).curHealth -= damage;

I don’t see your variable declarations, but ‘enemyHit’ appears to be a boolean based on line 3. And ‘hit’ does not have a gameObject, so you have to go through the transform or the collider.

components are items in the inspector (like transform, charactercontroller, rigidbody ect.)

so if you want to get a component then it should be like:

hit.transform.GetComponent<Health>().curHealth -= damage;

assuming offcourse that “Health” is a script you wish to get “curHealth” from.

I believe you could better do the hit.transform.tag in an if statement like so:

bool enemyhit ()
{

    if(hit.transform.tag == "enemy")
    {
       return true;
    }
    else
        return false;

}

but then you need to change your next if statement to

if(enemyhit())
{
   Hit.transform.GetComponent<Health>().curHealth -= damage; 
}

since you changed your Boolean into a Boolean method.

hmm… I don’t believe you need to call transform though… just hit.GetComponent().curHealth -= damage;