I have a game where I have an object that will fall out of the sky from a random pre defined transform, there are 72 transforms that this object can choose at random
When I first made the code for it I had the gameobject choose a spawn point (yes!) but then… All gameobjects being instantiated spawned from that same point.
Here is my code:
public class EnemySpawner : MonoBehaviour
{
public Transform spawnPointContainer; // Assign parent gameobject of spawn points.
private Transform[] spawnPoints;
int random;
[SerializeField] GameObject cubeEnemys;
[SerializeField] int maxCubeAmount;
[SerializeField] int currentCubeAmount;
Rigidbody rb;
void Awake()
{
CreateSpawnPointArray();
}
private void Update()
{
SpawnEnemyOnArray();
}
private void CreateSpawnPointArray()
{
// Initialize spawnPoints array.
spawnPoints = new Transform[spawnPointContainer.childCount];
// Populate array with child transforms.
for (int i = 0; i < spawnPointContainer.childCount; i++)
{
spawnPoints *= spawnPointContainer.GetChild(i);*
}
random = Random.Range(0, spawnPoints.Length);
Debug.Log(“There are: " + spawnPointContainer.childCount + " spawn points”);
}
// TODO , cubes only spawn a few at a time, at random points each, instead of the current blood fountain.
private void SpawnEnemyOnArray()
{
for (currentCubeAmount = 0; currentCubeAmount < maxCubeAmount; currentCubeAmount++)
{
if (currentCubeAmount >= maxCubeAmount)
{
return;
}
GameObject clone = Instantiate(cubeEnemys, spawnPoints[random].transform.position, transform.rotation) as GameObject;
Vector3 cubeVelocity = new Vector3(-1, -1, -1);
clone.GetComponent().AddForce(cubeVelocity, ForceMode.Force);
}
}
}
In case someone wants context, this is the “blood fountain” Imgur: The magic of the Internet
This image was taken as soon as the gameobjects start to spawn, probably like 3 seconds after starting the scene, by the time the gameobjects start to spawn there are over 1000 of them.
public class EnemySpawner : MonoBehaviour
{
public Transform spawnPointContainer; // Assign parent gameobject of spawn points.
private Transform spawnPoints;
int random;
[SerializeField] GameObject cubeEnemys;
[SerializeField] int maxCubeAmount;
[SerializeField] int currentCubeAmount;
Rigidbody rb;
void Awake()
{
CreateSpawnPointArray();
}
private void Update()
{
SpawnEnemyOnArray();
}
private void CreateSpawnPointArray()
{
// Initialize spawnPoints array.
spawnPoints = new Transform[spawnPointContainer.childCount];
// Populate array with child transforms.
for (int i = 0; i < spawnPointContainer.childCount; i++)
{
spawnPoints *= spawnPointContainer.GetChild(i);*
}
random = Random.Range(0, spawnPoints.Length);
Debug.Log(“There are: " + spawnPointContainer.childCount + " spawn points”);
}
// TODO , cubes only spawn a few at a time, at random points each, instead of the current blood fountain.
private void SpawnEnemyOnArray()
{
for (int i = 0; i < maxCubeAmount; currentCubeAmount++)
{
if (currentCubeAmount >= maxCubeAmount)
{
return;
}
else
{
foreach (Transform spawnPoint in spawnPoints)
{
GameObject clone = Instantiate(cubeEnemys, spawnPoints[random].transform.position, transform.rotation) as GameObject;
Vector3 cubeVelocity = new Vector3(-1, -1, -1);
clone.GetComponent().AddForce(cubeVelocity, ForceMode.Force);
}
}
}
}
}
_____________________________________________________________________________________
So I have played around with the code a little, I think I am beginning to understand foreach loops properly, this current version of the code still has gameobjects spawn at the start of the scene but there are 430 down from the 1300+
_____________________________________________________________________________________
I also don’t have “blood fountain” anymore, when the code is ran the first time I don’t get any more new spawns once garbage collection takes care of the ones that fly off the stage.
_____________________________________________________________________________________
Oooh, I’ve just realized that the amount of objects spawning is actually falling in line with “Max Cube Amount” as setting Max Cube Amount to 1 in the inspector is only spawning… God Damn it. 72 Cubes…
_____________________________________________________________________________________
sigh