Having trouble with Foreach Loops Unity 2020.3
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[i] = 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<Rigidbody>().AddForce(cubeVelocity, ForceMode.Force);
}
}
}
In case someone wants context, this is the "blood fountain" https://imgur.com/a/1DcV4wI 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.
Answer by ReceptiveRaptor · Mar 27, 2021 at 08:49 AM
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[i] = 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<Rigidbody>().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
Your answer
Follow this Question
Related Questions
c# foreach does not work 0 Answers
Retrieving values from list of Scriptable Objects 1 Answer
foreach raycast hit fmod parameter -0.2 f? 0 Answers
InvalidOperationException: Collection was modified; enumeration operation may not execute. 0 Answers
Foreach and GetComponentInChildren, getting NullReferenceException 1 Answer