Spawn Point

Hi guys, I have this spawn script that works fine, but I would like to add an OnTriggerEnter function where enemies only spawn when the player gets close to the spawnpoint. Can anyone help me do this? I would not be asking if i knew how to do it, but I’m getting desperate as it is the only thing not working properly. Cheers!

public class Spawn : MonoBehaviour
{
public GameObject zombie;
public float delayTime = 4f;

IEnumerator Start()
{
    var obj = Instantiate(zombie, transform.position, transform.rotation) as  GameObject;
    yield return new WaitForSeconds(delayTime);
    StartCoroutine(Start());
}

}

2 Answers

2

Put a collider component on an object that has this script, check IsTrigger checkbox on the component and put a tag “Player” on your players game object (in Inspector window, just above the Transform values).

public GameObject zombie;

public void OnTriggerEnter(Collider other)
{
        if ((other.tag == "Player")) //checks if it is Player that entered this trigger
        {
         var obj = Instantiate(zombie, transform.position, transform.rotation) as                GameObject;
        }             
}

Here’s one solution:

  1. Set the player’s tag to “Player”.

  2. Add a SphereCollider to your spawnpoint. Set the radius to the maximum distance at which it should detect the player. Check “Is Trigger”.

  3. Change Spawn.cs to:

    public class Spawn : MonoBehaviour {

     public GameObject zombie; 
     public float delayTime = 4f;
     private const string PlayerTag = "Player";
     private bool isPlayerNearby = false;
    
     void Start() {
         StartCoroutine(Spawn());
     }
    
     IEnumerator Spawn() {
         while (true) {
             if (isPlayerNearby) Instantiate(zombie, transform.position, transform.rotation) as  GameObject;
             yield return new WaitForSeconds(delayTime);
         }
     }
    
     void OnTriggerEnter(Collider other) {
         if (string.Equals(other.tag, PlayerTag)) isPlayerNearby = true;
     }
    
     void OnTriggerExit(Collider other) {
         if (string.Equals(other.tag, PlayerTag)) isPlayerNearby = false;
     }
    

    }

i don't why but something's gone wrong. i tried attaching the script to another spawnpoint and i got these two errors: error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement and error CS0542: `Spawn.Spawn()': member names cannot be the same as their enclosing type please help!

your script (class) is named Spawn and you have a method named the same in it. The method of a class with a same name is a special kind of method (constructor) thus you should avoid using the same name. Rename that Spawn method into something like SpawnEnemy. Remember to also change that name in StartCoroutine call in your Start method.

nvm, it's fixed now. it's working properly, thanks for your help guys.

double clicking on the error should take you to the line in code where compiler thinks the error occurred. That's the starting point of debugging any problem. It is probably complaining about that line where you are calling the StartCoroutine method because it needs a string as a parameter. So, something like this should fix the problem StartCoroutine("SpawnEnemy"); TonyLi probably wrote this code straight for his head, and didn't take time to double check things like this. It happens. ;)

jakovd's right -- sorry about that. Glad it's working now.