How can I have a script enable upon entering a certain area?

Ok so I am trying to make my enemy spawner work only when the player is within range and I cannot for the life of me get this to work, here is the code I have so far. Currently works correctly but spawns enemies from game start rather than a given area.

Enemy Spawner:

 #pragma strict
  public var enemyGhost : Transform;
  var spawnTimer : float;
  var Health = 100;
  var spawnTime = 5;
  function Awake()
  function Update()
  	spawnTimer += Time.deltaTime;
      if (spawnTime < spawnTimer){
          Instantiate(enemyGhost, transform.position, transform.rotation);
          spawnTimer = 0;
      if(Health <= 0){
  function ApplyDamage (TheDamage : int){
      Health -= TheDamage;
  function Dead (){
      Destroy (gameObject);

And here is the code I have been trying to get to activate the script when the player enters a collider.

//attach this script to your trigger
     var Player : GameObject;
     var Script : EnemySpawn;
     function OnTriggerEnter(other : Collider) //Check if something has entered the trigger ( and declares this object in "other" )
     if(other.GetComponent.<Collider>().tag == Player.tag) //Checks if the Player is inside the trigger
     Script.enabled = true; //enables theScript.

The script I am trying to use as a trigger is currently a gameObject with a Sphere Collider set to ‘is trigger’ and the script attached.

Thanks in advance.


Whatever I understand so far is that Your code is working, but it starts working as the scene loaded but you want to instantiate only the time when you entered any particular area.

So please check first that whether you are in that particular region or not.
Easiest approach is :-

  • Place a very big Sphere/Box collider on that area in which you want to do that.
  • Making making that collider isTriggered.
  • And write the code in OnTriggeredEnter()
  • You can see the little reference from Here.
  • List item

But there are number others methods and ways to achieve this. But please first check and let us know if you need any thing else.