Chapter 10 Gun.processHit() question

private void processHit(GameObject hitObject) 
{
  if (hitObject.GetComponent<Player>() != null) 
  {
    hitObject.GetComponent<Player>().TakeDamage(damage);
  }

  if (hitObject.GetComponent<Robot>() != null) 
  {
    hitObject.GetComponent<Robot>().TakeDamage(damage);
  }
}

I don’t understand what this code using for:

if (hitObject.GetComponent<Player>() != null) 
{
    hitObject.GetComponent<Player>().TakeDamage(damage);
}

In what case (hitObject.GetComponent<Player>() != null) can happens and why we hit self?

It so you take damage from the robot’s rockets. And it only damage the object the player.

This topic was automatically closed after 166 days. New replies are no longer allowed.