on task 6 of Challenge 4 - Soccer Scripting - Unity Learn I have got it so 1 more enemy spawns in each wave, but the first wave should have 1 enemy but 2 spawn.
This is my code I can’t understand why 2 spawn and how to get 1 to spawn.
public class SpawnManagerX : MonoBehaviour
{
public GameObject enemyPrefab;
public GameObject powerupPrefab;
private float spawnRangeX = 10;
private float spawnZMin = 15; // set min spawn Z
private float spawnZMax = 25; // set max spawn Z
public int enemyCount;
public int waveCount = 1;
public GameObject player;
// Update is called once per frame
void Update()
{
enemyCount = GameObject.FindGameObjectsWithTag("Enemy").Length;
if (enemyCount == 0)
{
waveCount++;
SpawnEnemyWave(waveCount);
}
}
// Generate random spawn position for powerups and enemy balls
Vector3 GenerateSpawnPosition ()
{
float xPos = Random.Range(-spawnRangeX, spawnRangeX);
float zPos = Random.Range(spawnZMin, spawnZMax);
return new Vector3(xPos, 0, zPos);
}
void SpawnEnemyWave(int enemiesToSpawn)
{
Vector3 powerupSpawnOffset = new Vector3(0, 0, -15); // make powerups spawn at player end
// If no powerups remain, spawn a powerup
if (GameObject.FindGameObjectsWithTag("Powerup").Length == 0) // check that there are zero powerups
{
Instantiate(powerupPrefab, GenerateSpawnPosition() + powerupSpawnOffset, powerupPrefab.transform.rotation);
}
// Spawn number of enemy balls based on wave number
for (int i = 0; i < enemiesToSpawn; i++)
{
Instantiate(enemyPrefab, GenerateSpawnPosition(), enemyPrefab.transform.rotation);
}
ResetPlayerPosition(); // put player back at start
}
// Move player back to position in front of own goal
void ResetPlayerPosition ()
{
player.transform.position = new Vector3(0, 1, -7);
player.GetComponent<Rigidbody>().velocity = Vector3.zero;
player.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
}
}
Where are you changing waveNumber? Since you have it set as a public variable, Unity has probably serialized the value you had assigned initially. So if you’re changing it in the code now, the game is still using the value you had before. Select the GameObject the script is assigned to and check/change the value of waveNumber there.
I’ve changed it in the inspector not in the actual script. If I change it in the script same thing happens. When I press play the number in inspector adds 1 to what I set it to. So it does spawn the number of enemies the inspector but it spawns 1 more than I set it to.
That is exactly what your code is doing if enemyCount is 0, see my previous message. Please share a screenshot of your Inspector settings. You can use Debug.Log to confirm https://discussions.unity.com/t/748729/14
Very strange I did the next step to get enemy balls to roll towards the players goal and it started spawning the correct number of enemies. I have no idea why.
The worst thing you could do for yourself right now would be to move on without understanding why what’s happening is happening. This is a great opportunity to learn a practical understanding from an actual experience. Keep digging at it until you understand how it works! Otherwise you’ll soon find yourself in the same situation when the next obstacle arises.
that makes sense but I’m near enough certain that it just didn’t work at first but does now. I know doesn’t make sense so later when I go on it later I’ll post the code that I did last night which worked straight away to see if anyone can see any differences in it.
Ok so I am missing something in the script last night that caused it to spawn 2 enemies instead of 1. So just to clarify this script here is the correct script to spawn 1 enemy the scripts in first post spawns 2.
public class SpawnManagerX : MonoBehaviour
{
public GameObject enemyPrefab;
public GameObject powerupPrefab;
private float spawnRangeX = 10;
private float spawnZMin = 15; // set min spawn Z
private float spawnZMax = 25; // set max spawn Z
public int enemyCount;
public int waveCount = 1;
public GameObject player;
// Update is called once per frame
void Update()
{
enemyCount = GameObject.FindGameObjectsWithTag("Enemy").Length;
if (enemyCount == 0)
{
SpawnEnemyWave(waveCount);
}
}
// Generate random spawn position for powerups and enemy balls
Vector3 GenerateSpawnPosition ()
{
float xPos = Random.Range(-spawnRangeX, spawnRangeX);
float zPos = Random.Range(spawnZMin, spawnZMax);
return new Vector3(xPos, 0, zPos);
}
void SpawnEnemyWave(int enemiesToSpawn)
{
Vector3 powerupSpawnOffset = new Vector3(0, 0, -15); // make powerups spawn at player end
// If no powerups remain, spawn a powerup
if (GameObject.FindGameObjectsWithTag("Powerup").Length == 0) // check that there are zero powerups
{
Instantiate(powerupPrefab, GenerateSpawnPosition() + powerupSpawnOffset, powerupPrefab.transform.rotation);
}
// Spawn number of enemy balls based on wave number
for (int i = 0; i < enemiesToSpawn; i++)
{
Instantiate(enemyPrefab, GenerateSpawnPosition(), enemyPrefab.transform.rotation);
}
waveCount++;
ResetPlayerPosition(); // put player back at start
}
// Move player back to position in front of own goal
void ResetPlayerPosition ()
{
player.transform.position = new Vector3(0, 1, -7);
player.GetComponent<Rigidbody>().velocity = Vector3.zero;
player.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
}
}
I am now going to look at what differences there is.