3
\$\begingroup\$

I have a spawn script that so far only creates one Enemy spawn point and enemy trigger while only spawning one enemy.

Instead, I want it to create multiple spawns and triggers, but keep it such that only one enemy spawns at a time, at a random on of the spawn points.

How can I do this?


Here's the relevant code at the moment:

public class SpawnManager : MonoBehaviour {

    public GameObject Enemy; // the enemy prefab
    public float mytimer; // the time to wait before spawn

    private GameObject _spawndEnemy; // the enemy that was spawnd

    void SpawnEnemy()
    {
        var enemySpawnPoint =  GameObject.Find("FFEnemySpawn1").transform;
        _spawndEnemy = Instantiate(Enemy, enemySpawnPoint.position, enemySpawnPoint.rotation) as GameObject;
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.name == "FFMirror1") {
            mytimer = Random.Range(0,15);
            Destroy(_spawndEnemy);
            Invoke("SpawnEnemy", mytimer);
            Debug.Log("Spawn Normal");
        }

    }
}
\$\endgroup\$
  • \$\begingroup\$ "keep it where as to it only spawns"—definitely some accidental extra words in there. What do you mean? \$\endgroup\$ – Anko Dec 14 '14 at 21:52
  • \$\begingroup\$ I'm sorry, I only want one enemy on the stage but I want the enemy to spawn on different spawning locations when the spawn is triggered. \$\endgroup\$ – Ghostdre Dec 14 '14 at 21:57
  • \$\begingroup\$ So you want it to spawn in, say, a random position when it's triggered? \$\endgroup\$ – jzx Dec 14 '14 at 22:00
  • \$\begingroup\$ Not necessarily random positions, let me better explain. \$\endgroup\$ – Ghostdre Dec 14 '14 at 22:01
  • 1
    \$\begingroup\$ I walk into a room with a trigger in it. That trigger will activate a spawn timer which will spawn a enemy. Now what Im stuck on is actually going into another room with a trigger and spawn in a different location. Hopefully that better explains it. \$\endgroup\$ – Ghostdre Dec 14 '14 at 22:09
3
\$\begingroup\$

This falls under the Single Responsibility Principle (see SOLID).

Specifically:

  • the spawn point should handle entity creation, rather than a manager
  • you would want to either have a field on the spawn point or a parameter on the spawn method that would tell it what to spawn
  • you would then use an ordinary trigger behavior (maybe called TriggerSpawn, with a target array field) to trigger the spawn point
  • tag the spawned entity so that you can locate it later, rather than referencing it within the entity that created it
  • once the entity is spawned, you can disable the trigger until some condition is met (the entity could enable the trigger if it's destroyed), or you could disable the spawn points, etc.
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

I got this random spawning code I made if it helps. I'm sure its not hard to have it spawning one person at a time.

    public class SpawnScript : MonoBehaviour
{
    public GameObject player;

    void Start()
    {
        GameObject theSpawn;

        do  // checks an error
        {
            getSpawn(gameObject); // grabs random spawn
            theSpawn = getSpawn(gameObject);
        } while (theSpawn == gameObject);

        player.transform.position = theSpawn.transform.position; // changes players position
    }

    private GameObject getSpawn(GameObject spawn)
    {
        Transform[] childs; // spawn list childs transform
        GameObject randomSpawn; // random spawn game object

        childs = (Transform[])spawn.GetComponentsInChildren<Transform>(); // gets transform of spawnPositions children
        randomSpawn = (GameObject)((Transform)childs[Random.Range(0, childs.Length)]).gameObject; // randomly picks a childs transform

        return randomSpawn;
    }
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.