How to make enemy tp to you after 45 sec.?

How to make my enemy in this game teleport to me after 45 sec. or something?
I used this code, to make him teleport to me:


    using UnityEngine;
    using System.Collections;
    
    public class Enemy : MonoBehaviour {
    	
    	 public float distanceToPlayer = 5F;
    
        public float minTimeInView = 1F;
        public float maxTimeInView = 1F;
    
        private Transform cam;
    
        public void Spawn () {
    
            StartCoroutine ( RandomEncounter () );
    		
    		}
    
        void Start () {
    		
    		cam = Camera.mainCamera.transform;
    		
    		 renderer.enabled = false;
    
            Spawn ();
    
        }
    
        private IEnumerator RandomEncounter () {
    
    		renderer.enabled = true;
    		
    		Vector3 pos = cam.forward;
            pos *= distanceToPlayer;
            pos += cam.position;
    
            transform.position = pos;
    		
    		yield return new WaitForSeconds (Random.Range (minTimeInView, maxTimeInView));
    
    		renderer.enabled = false;
    		
    		 }
    
    }

This code seems ok - are you having any problem? Anyway, disabling the renderer only makes the enemy invisible - it may attack the player, or the player can collide with it. Is this an intended behaviour? Another point: disabling the renderer can leave childed objects still visible - if the enemy has a weapon childed to it, you may have a “haunted weapon” effect, where it moves by itself and attacks the player!

If you want to make the object completely invisible, including any children, use something like this:


  ...
  Renderer[] rendrs; // holds a list of renderers in the object and children

  void SetVisible(bool onOff){
    // sets all of them visible/invisible, according to onOff:
    foreach (Renderer rendr in rendrs) rendr.enabled = onOff;
  }

  void Start () {
    cam = Camera.mainCamera.transform;
    rendrs = GetComponentsInChildren< Renderer>();
    SetVisible(false);		
    Spawn ();
  }
    
  private IEnumerator RandomEncounter () {
    SetVisible(true);
    Vector3 pos = cam.forward;
    pos *= distanceToPlayer;
    pos += cam.position;
    transform.position = pos;
    yield return new WaitForSeconds (Random.Range (minTimeInView, maxTimeInView));
    SetVisible(false);
  }
}