Game not using code? What?

This is from EnemySpawner.cs, but reworked to accept formations. For some reason the game is evidently reading the code, but is actually not using them somehow, and is behaving as if those values don’t exist. Also no errors appear in the console, so I don’t know what I’m doing wrong here. Help?

NOTE: Enemies DO spawn, shoot, and disappear as intended, but again they’re not spawning in the correct coordinates.

private IEnumerator SpawnAllEnemiesInWaves(WaveConfig waveConfig)
    {
        float yPosRandomFactor = waveConfig.GetSpawnPlaceRNG();
        float yPosRandomizer = Random.Range(yPosRandomFactor, -yPosRandomFactor);
        int formationCount = 0;

        var formationPos = waveConfig.GetFormation()[formationCount].transform.position;
        var waypointPos = waveConfig.GetWaypoints()[0].transform.position;
        var spawnPoint = (Vector2)(waypointPos + formationPos);

        float spawnTimer = waveConfig.GetTimeBetweenSpawns();
        float spawnTimerRandomizer = waveConfig.GetTimeBetweenSpawnsRNG();
        float overallSpawnTimer = Random.Range(
            spawnTimer + spawnTimerRandomizer,
            spawnTimer - spawnTimerRandomizer);
        spawnPoint.y += yPosRandomizer;

        yield return new WaitForSeconds(waveConfig.GetFloatDelay());
        
        for (int waveEnemyCount = 0; waveEnemyCount < waveConfig.GetEnemyQuantity(); waveEnemyCount++)
        {
            Debug.Log(waveConfig.GetFormation()[formationCount].transform.position);

            var newEnemy = Instantiate(
                   waveConfig.GetEnemyPrefab(),
                   spawnPoint,
                   Quaternion.identity);
            Debug.Log(newEnemy.name + " spawned.");
            //Debug.Log(newEnemy.GetComponent<EnemyPath>());
            //Debug.Log(++waveEnemyCount);
            newEnemy.GetComponent<EnemyPath>().SetWaveConfig(waveConfig);

            formationCount++;
            if (formationCount > waveConfig.GetFormation().Count - 1) { formationCount = 0; }
            formationPos = waveConfig.GetFormation()[formationCount].transform.position;

            yield return new WaitForSeconds(overallSpawnTimer);
        }
        
    }

I can’t see the rest of your code, but… you are starting your co-routine somewhere?

The coroutine is in fact starting. Otherwise I would’ve specified the enemies aren’t spawning.

Sorry, I didn’t see the note!

This doesn’t look quite right. Try:

Vector2 spawnPoint = waypointPos + formationPos;

assuming formationPos and waypointPos are both vectors.

If that doesn’t make a difference, it’s worth Debug.Log(ging) spawnPoint to the console to see whether it’s been calculated correctly

No worries. The note was added after your post.

Also solution didn’t work. Any modification to spawnPoint seems to take no effect for some reason, but the game reads them correctly anyhow.

EDIT: Okay. ANY modifications I make to spawnPoint changes NOTHING about where the ship spawns! What gives?!?

EDIT 2: This tells me I shouldn’t be incorporating the formations in EnemySpawner, but rather EnemyPath. Let’s see if I have luck there.

That makes sense - you’re going to need to maintain the formation at every waypoint, not just where the enemy spawns.

Looking at my project, there is a line in the Start method of the EnemyPath script that sets the transform.position of the enemy to the waypoint at index 0.

If that’s in your code, then EnemySpawner is setting the enemy to instantiate at the spawnPoint, then as soon as it is instantiated, the EnemyPath script is immediately overwriting the posiiton with that in the waveConfig.

This bug is in mine too, but because both the EnemySpawner and EnemyPath script are setting the transform to the same place, I hadn’t noticed it before.

I fixed the problem days ago, but you’re right. I don’t know where I fumbled the code to make EnemyPath override EnemySpawner’s vector2, but I managed to bypass the issue by making EnemyPath do most of the hard work, while making extra methods to make the formation mechanic function.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms