Help FindWithTag Object reference not set to an instance of an object

Hello Problem this Script :

 var Model : Transform;
var target : Transform;
 

 
 function Update () {
  if(!Model){
  Model = GameObject.FindWithTag ("Player").transform; 
 target.transform.LookAt(Model);
       
} 

when models object is destroyed, it generates this error:
NullReferenceException: Object reference not set to an instance of an object
this is because object models was destroyed and the script tries to transform absence.
I also tried something like that but without success:

 function Update () {
 if (models != null){
     models = GameObject.FindWithTag ("Player").transform; 
  target.transform.LookAt(models);
         models =  models.transform;
     }
     else
     {
         return;
     }
 }

anyone knows the correct solution this problem thanks in advance?

You have the correct approach when using if (models != null){ however if your FindWithTag is inside that condition it is never going to be able to find models.

models = GameObject.FindWithTag ("Player").transform;

if (models != null){
target.transform.LookAt(models);
}

Something like that is what you want but obviously you want to be avoiding using FindWithTag in update unless you actually need to. You should be finding the Player in start and caching the reference to the gameobject and then just getting its transform in update to track the position. I am also not clear why you have this in there after the look at: models = models.transform;

the code you have brought and correct and works perfectly
Thank you’re a big thank you or finally thank you or solved very many thanks