Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Problem with EnemyWaveManger combined with EnemyMovement

Discussion in 'Scripting' started by Myspritzz, Feb 6, 2023.

  1. Myspritzz

    Myspritzz

    Joined:
    May 29, 2022
    Posts:
    2
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class EnemyMovement : MonoBehaviour
    {
    //public GameObject enemyObject;
    private List<Vector2Int> pathCells;
    private GameObject enemyInstance;
    private int nextPathCellIndex;
    private bool enemyRunCompleted;

    private void Start()
    {
    nextPathCellIndex = 1;
    enemyRunCompleted = false;
    }

    private void Update()
    {
    if (pathCells != null && pathCells.Count > 1 && !enemyRunCompleted && enemyInstance != null)
    {
    Vector3 currentPos = enemyInstance.transform.position;
    Vector3 nextPos = new Vector3(pathCells[nextPathCellIndex].x, 0.2f, pathCells[nextPathCellIndex].y);
    enemyInstance.transform.position = Vector3.MoveTowards(currentPos, nextPos, Time.deltaTime * 2);
    if (Vector3.Distance(currentPos, nextPos) < 0.05f)
    {
    if (nextPathCellIndex >= pathCells.Count - 1)
    {
    enemyRunCompleted = true;
    }
    else
    {
    nextPathCellIndex++;
    }
    }
    }
    }



    public void SetPathCells(List<Vector2Int> pathCells)
    {
    this.pathCells = pathCells;
    }


    }


    this is my script with for enemyMovement
    SetPathCells generate a route for my enemys (ps. my map generate everytime random ( this script alone is working if i instantiate one enemy in here

    public class EnemyWaveManager : MonoBehaviour
    {
    public enum SpawnState { SPAWNING, WAITING, COUNTING};

    [System.Serializable]
    public class Wave
    {
    public string name;
    public Enemy[] enemies;
    }

    [System.Serializable]
    public class Enemy
    {
    public GameObject enemyInstance;
    public int count;
    public float rate;
    }

    public Wave[] waves;
    private int nextWave = 0;
    public float timeBetweenWaves = 5f;
    public float waveCountdown;
    private float searchTimer = 1f;
    private SpawnState state = SpawnState.COUNTING;
    //private EnemyMovement enemyMovement;

    private void Start()
    {
    waveCountdown = timeBetweenWaves;
    }

    private void Update()
    {
    if (state == SpawnState.WAITING)
    {
    if (!EnemyIsAlive())
    {
    beginNewRound();
    }
    else
    {
    return;
    }
    }

    if (waveCountdown <= 0)
    {
    if (state != SpawnState.SPAWNING)
    {
    StartCoroutine(SpawnWave(waves[nextWave]));
    }
    }
    else
    {
    waveCountdown -= Time.deltaTime;
    }
    }

    private void beginNewRound()
    {
    Debug.Log("Wave Completed");

    state = SpawnState.COUNTING;
    waveCountdown = timeBetweenWaves;

    if (nextWave + 1 > waves.Length - 1)
    {
    nextWave = 0;
    Debug.Log("Completed all Waves");
    }
    else
    {
    nextWave++;
    }
    }

    private bool EnemyIsAlive()
    {
    searchTimer -= Time.deltaTime;
    if (searchTimer <= 0f)
    {
    searchTimer = 1f;
    if (GameObject.FindGameObjectsWithTag("Enemy") == null)
    {
    return false;
    }
    }
    return true;
    }

    private IEnumerator SpawnWave (Wave _wave)
    {
    Debug.Log("Spawning Wave:" + _wave.name);
    state = SpawnState.SPAWNING;

    for (int i = 0; i < _wave.enemies.Length; i++)
    {
    var temp = _wave.enemies;

    for (int j = 0; j < temp.count; j++)
    {
    SpawnEnemy(temp.enemyInstance);
    yield return new WaitForSeconds(1f / temp.rate);
    }
    }
    state = SpawnState.WAITING;
    yield break;
    }

    private void SpawnEnemy(GameObject _enemy)
    {
    Instantiate(_enemy, new Vector3(0, 0.2f, 5f), Quaternion.identity);
    Debug.Log("Spawning Enemy:" + _enemy.name);
    }
    }


    this is my script for setting up waves
    this script is spawning the enemys correctly but the enemys dont move
    i have added the Movement script to my enemy Prefab
    and i added the enemy Prefab to the wavemanager.

    btw im new to programming so sorry for my problem.
    im already searching 2 weeks to solve this problem.
    thanks for every help i get.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,622
    Please edit your post to use code-tags.

    Are you aware that you've not actually stated the problem or what debugging you've done?
     
  3. Myspritzz

    Myspritzz

    Joined:
    May 29, 2022
    Posts:
    2
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class EnemyMovement : MonoBehaviour
    7. {
    8. //public GameObject enemyObject;
    9. private List<Vector2Int> pathCells;
    10. private GameObject enemyInstance;
    11. private int nextPathCellIndex;
    12. private bool enemyRunCompleted;
    13.  
    14. private void Start()
    15. {
    16. nextPathCellIndex = 1;
    17. enemyRunCompleted = false;
    18. }
    19.  
    20. private void Update()
    21. {
    22. if (pathCells != null && pathCells.Count > 1 && !enemyRunCompleted && enemyInstance != null)
    23. {
    24. Vector3 currentPos = enemyInstance.transform.position;
    25. Vector3 nextPos = new Vector3(pathCells[nextPathCellIndex].x, 0.2f, pathCells[nextPathCellIndex].y);
    26. enemyInstance.transform.position = Vector3.MoveTowards(currentPos, nextPos, Time.deltaTime * 2);
    27. if (Vector3.Distance(currentPos, nextPos) < 0.05f)
    28. {
    29. if (nextPathCellIndex >= pathCells.Count - 1)
    30. {
    31. enemyRunCompleted = true;
    32. }
    33. else
    34. {
    35. nextPathCellIndex++;
    36. }
    37. }
    38. }
    39. }
    40.  
    41.  
    42.  
    43. public void SetPathCells(List<Vector2Int> pathCells)
    44. {
    45. this.pathCells = pathCells;
    46. }
    47.  
    48.  
    49. }
    50.  




    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class EnemyWaveManager : MonoBehaviour
    7. {
    8.    public enum SpawnState { SPAWNING, WAITING, COUNTING};
    9.  
    10.     [System.Serializable]
    11.     public class Wave
    12.     {
    13.         public string name;
    14.         public Enemy[] enemies;
    15.     }
    16.  
    17.     [System.Serializable]
    18.     public class Enemy
    19.     {
    20.         public GameObject enemyInstance;
    21.         public int count;
    22.         public float rate;
    23.     }
    24.  
    25.     public Wave[] waves;
    26.     private int nextWave = 0;
    27.     public float timeBetweenWaves = 5f;
    28.     public float waveCountdown;
    29.     private float searchTimer = 1f;
    30.     private SpawnState state = SpawnState.COUNTING;
    31.     //private EnemyMovement enemyMovement;
    32.  
    33.     private void Start()
    34.     {
    35.         waveCountdown = timeBetweenWaves;
    36.     }
    37.  
    38.     private void Update()
    39.     {
    40.         if (state == SpawnState.WAITING)
    41.         {
    42.             if (!EnemyIsAlive())
    43.             {
    44.                 beginNewRound();
    45.             }
    46.             else
    47.             {
    48.                 return;
    49.             }
    50.         }
    51.  
    52.         if (waveCountdown <= 0)
    53.         {
    54.             if (state != SpawnState.SPAWNING)
    55.             {
    56.                 StartCoroutine(SpawnWave(waves[nextWave]));
    57.             }
    58.         }
    59.         else
    60.         {
    61.             waveCountdown -= Time.deltaTime;
    62.         }
    63.     }
    64.  
    65.     private void beginNewRound()
    66.     {
    67.         Debug.Log("Wave Completed");
    68.  
    69.         state = SpawnState.COUNTING;
    70.         waveCountdown = timeBetweenWaves;
    71.  
    72.         if (nextWave + 1 > waves.Length - 1)
    73.         {
    74.             nextWave = 0;
    75.             Debug.Log("Completed all Waves");
    76.         }
    77.         else
    78.         {
    79.             nextWave++;
    80.         }
    81.     }
    82.  
    83.     private bool EnemyIsAlive()
    84.     {
    85.         searchTimer -= Time.deltaTime;
    86.         if (searchTimer <= 0f)
    87.         {
    88.             searchTimer = 1f;
    89.             if (GameObject.FindGameObjectsWithTag("Enemy") == null)
    90.             {
    91.                 return false;
    92.             }
    93.         }
    94.         return true;
    95.     }
    96.  
    97.     private IEnumerator SpawnWave (Wave _wave)
    98.     {
    99.         Debug.Log("Spawning Wave:" + _wave.name);
    100.         state = SpawnState.SPAWNING;
    101.  
    102.         for (int i = 0; i < _wave.enemies.Length; i++)
    103.         {
    104.             var temp = _wave.enemies[i];
    105.  
    106.             for (int j = 0; j < temp.count; j++)
    107.             {
    108.                 SpawnEnemy(temp.enemyInstance);
    109.                 yield return new WaitForSeconds(1f / temp.rate);
    110.             }
    111.         }
    112.         state = SpawnState.WAITING;
    113.         yield break;
    114.     }
    115.  
    116.     private void SpawnEnemy(GameObject _enemy)
    117.     {
    118.         Instantiate(_enemy, new Vector3(0, 0.2f, 5f), Quaternion.identity);
    119.         Debug.Log("Spawning Enemy:" + _enemy.name);
    120.     }
    121. }
    122.  

    if i spawn a enemy he isnt moving at all.
    tbh i dont have done debugging cause i dont know how.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    Time to learn! It's not optional if you're going to write code. Writing code means you WILL write bugs, so debugging is part and parcel of the process of writing code.

    Once you figure out what is happening, here is how to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    Here's how to get started debugging:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    When in doubt, print it out!(tm)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.