rotate object when player not looking

Hi,

I want to make the object rotate when the player is not looking at it, i already have the script but it’s not working

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LookingAtPlayer: MonoBehaviour
{
	public Transform player;

	void OnBecamevisible()
	{

	}
	void OnBecameinvisible()
	{
		Vector3 playerPosition = new Vector3 (player.transform.position.x, transform.position.y, player.transform.position.z);
		transform.LookAt (playerPosition);
	}
}

reference

I think i fix it, i just had to identify the gameobject itself too

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LookingAtPlayer: MonoBehaviour
{

	MeshFilter meshFilter;
	public GameObject mesh;
	public Transform player;

	void OnBecameVisible()
	{
	}


	void OnBecameInvisible()
	{
		Vector3 playerPosition = new Vector3 (player.transform.position.x, transform.position.y, player.transform.position.z);
		transform.LookAt (playerPosition);
	}
}

if you have any better answer please add them too