Accessing script after using findobjectwithtag

Hi I am trying to access the PlayerHealth script under the player in my game from the enemies but I keep getting the Null Reference Exception error :

public class HornedDemonTouchDamage : MonoBehaviour
{
  public GameObject player;
  PlayerHealth playerHealth;

void Start()
    {       
          playerHealth = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerHealth>();
     }
}

is this correct way to do it? Thanks!

your syntax seems correct, you probably dont have your player tagged as “Player”

you can also use

GameObject.Find("Player").GetComponent<PlayerHealth>();

if the name of your player object is - Player

If there is only one in the scene, you could useplayerHealth = FindObjectOfType<PlayerHealth>();instead and then it doesn’t matter what the gameobject name is.

make it public and then play the game. then you can see if player health is filled or not and then post again…

Thanks! still not sure why findobjectwithtag doesnt work thou. The player is tagged and the PlayerHealth script is running but FindObjectOfType is working now.