The question is answered, right answer was accepted
[SOLVED]How do I spawn GameObject[] in Transform[]?
I have 2 arrays: GameObject[] enemy; Transform[] spawns;
How do I randomly spawn enemies at random spawn point?
Edit: I was looking for this: GameObject temp = (GameObject)Instantiate(enemy[(Random.Range(0, 2))], spawns[(Random.Range(0, 4))].transform.position, Quaternion.identity);
Answer by Commoble · Mar 17, 2017 at 03:28 PM
Instantiate creates a copy of a prefab or an existing gameobject. The UnityEngine.Random static class is a simple way to get random numbers.
Something like this? void SpawnEnemy() { GameObject temp = (GameObject)Instantiate(enemy[Random.Range(0,2)], spawn[Random.Range(0, 4)], Quaternion.identity); }
Answer by Ahsenhein · Mar 17, 2017 at 07:30 PM
I'm a beginner too but I think if you use a navmesh and prebake everything then you just need to create a loop and set a random location(with Random.Range()) to the agent and then ask if It's a valid path by using NavMeshAgent.HasPath. Then If true store this coordinate in your Transform[] spawns;
I'm currently new to Nav$$anonymous$$eshAgent. Can you give me an example of your code if you have one?
Something like this
float posX = enemy.transform.position.x + Random.Range(-5, 5);
float posZ = enemy.transform.position.z + Random.Range(-5, 5);
Vector3 randomPos = new Vector3(posX, enemy.transform.position.y, posZ);
Nav$$anonymous$$eshPath path = new Nav$$anonymous$$eshPath();
if (enemy.nav$$anonymous$$eshAgent.CalculatePath(randomPos, path))
{
if (enemy.nav$$anonymous$$eshAgent.hasPath)
{
and so on... but this is complicated for us. I think you should just create empty game objects as spawnpoints and spread them all over the place and then pick one randomly. On start you can feed your array with their locations. and use a loop to get one random index.