Trying to move an object from a struct list

Hi I am trying to make a space invaders type game using only one monobehavior script. Yes I know it’s not practical, its for fun.

I am using a struct to define enemy instances from a scriptable object, there are 4 types of enemy scriptobjs only differing in speed. First I instantiate an enemy pool of size X, randomly selecting from each type of enemy and adding it to a list of Enemy structs. Then I loop through the Enemy list and move the indexed transform according to its speed defined in the struct, however I see no results on the screen. It was in a working state at one point but I made some changes and it seems no matter what I do it wont fix itself.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GM : MonoBehaviour
{
    // Begin definitions alphasort

    [SerializeField] float _fSpawnTimeSeconds;
    [SerializeField] int _iEnemyPoolInitSize;

    [SerializeField] List<EnemyScriptObj> _lEnemyScriptObj = new List<EnemyScriptObj>();
    List<Enemy> _lActiveEnemies = new List<Enemy>();
    List<Enemy> _lEnemyPool = new List<Enemy>();

    public struct Enemy
    {
        public EnemyScriptObj stats;
        public GameObject obj;
        public Enemy(EnemyScriptObj stats, GameObject obj)
        {
            this.stats = stats;
            this.obj = obj;
        }
    }

    [SerializeField] Transform _tEnemyPoolContainer, _tActiveEnemyContainer;

    Vector3Int NewPos()
    {
        return new Vector3Int(Random.Range(-12, 12), 9, 0);
    }

    // End definitions, begin methods alphasort

    void Start()
    {
        InitializeEnemyPool();
        StartCoroutine(StartSpawning(true));
    }

    void Update()
    {
        MoveEnemies();
    }

    void InitializeEnemyPool()
    {
        for (int i = 0; i <= _iEnemyPoolInitSize; i++)
        {
            int typeIndex = Random.Range(0, _lEnemyScriptObj.Count);
            EnemyScriptObj currentEnemy = _lEnemyScriptObj[typeIndex];
            Enemy enemy = new Enemy(currentEnemy, Instantiate(currentEnemy.go, NewPos(), Quaternion.identity));
            _lEnemyPool.Add(enemy);
            enemy.obj.transform.parent = _tEnemyPoolContainer;
        }
    }

    void MoveEnemies()
    {
        foreach (Enemy enemy in _lActiveEnemies)
        {
            Vector3 pos = enemy.obj.transform.position;
            pos += enemy.stats.speed * Time.deltaTime * Vector3.down;
            Debug.Log(enemy.obj.name + ", " + enemy.stats.speed + ", " + pos);
            if (pos.y < -10)
                pos = NewPos();
        }
    }

    void SpawnEnemy()
    {
        int randomIndex = Random.Range(0, _lEnemyPool.Count);
        Enemy enemy = _lEnemyPool[randomIndex];
        Transform t = enemy.obj.transform;
        t.parent = _tActiveEnemyContainer;
        _lEnemyPool.Remove(enemy);
        _lActiveEnemies.Add(enemy);
    }

    IEnumerator StartSpawning(bool spawning)
    {
        while (_lEnemyPool.Count > 0 && spawning)
        {
            SpawnEnemy();
            yield return new WaitForSeconds(_fSpawnTimeSeconds);
            if (_lEnemyPool.Count <= 0)
                spawning = false;
        }
        yield return null;
    }
}

At a loss here any help would be appreciated! Everything is properly assigned in the inspector I made very sure of it, no compiler errors either

Edit: Each enemy is moving but seems like its position is being reset every frame. The NewPos() method does not have anything to do with it after debugging

You’re not setting the newly calculated pos vector back to transform.position

1 Like

Ahhh I see, I was setting a temporary variable but not assigning it to the actual one. Appreciate the help!

1 Like