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. Dismiss Notice

Changing instantiate order in game

Discussion in 'Scripting' started by haqpod, Mar 9, 2022.

  1. haqpod

    haqpod

    Joined:
    Feb 4, 2020
    Posts:
    14
    Hi, I am making a game where I instantiate the players units at the start of each round (yes perhaps it should be pooled). Issue is that I would like for the player to be able to change the order of when units spawn in game. There will be 20 or more available units, so I need a manageable way to do it. Perhaps it could be done with a list system, though I am stuck on how more precisely that would be done.
    Thanks in advance for any help.

    Code (CSharp):
    1. public void EnterBattleStart()
    2.     {
    3.        for (int i = 0; i < elvenWarriors; i++)
    4.         {
    5.             Instantiate(elvenWarriorPlayer, playerSpawnPosition, Quaternion.identity);
    6.             playerSpawnPosition -= new Vector3(0, 1);
    7.         }
    8.         for (int i = 0; i < dwarvenWarriors; i++)
    9.         {
    10.             Instantiate(dwarfWarriorPlayer, playerSpawnPosition, Quaternion.identity);
    11.             playerSpawnPosition -= new Vector3(0, 1);
    12.         }
    13.         for (int i = 0; i < adventurers; i++)
    14.         {
    15.             Instantiate(adventurerPlayer, playerSpawnPosition, Quaternion.identity);
    16.             playerSpawnPosition -= new Vector3(0, 1);
    17.         }
    18.     }
     
  2. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Is there any particular reason you're using a for loop to spawn these characters? Just curious because I would probably use a list to do this as well, you could use an integer or grab the index in the list to spawn what you want.
     
  3. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    Hi, have a look at the command pattern, Charles from Infallible Code did a great video on the subject recently :



    Instead of directly calling your spawning logic, you embed it cleanly in a command object with all the data you need like which amount of enemies and which type so that it can perform the spawning.

    Then you setup your UI to let your player configure his spawns and you generate the list of command object from that.
     
  4. haqpod

    haqpod

    Joined:
    Feb 4, 2020
    Posts:
    14
    thanks ill check it out
     
  5. haqpod

    haqpod

    Joined:
    Feb 4, 2020
    Posts:
    14
    So I guess I could make a list of gameobjects, and then instantiate the number of units the player has? But I need to instantiate them beneath each other, so will need to increment y -2 each time. Not sure how I could spawn like I do in my loop using a list.
     
  6. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    That's the thing I wouldn't use a for loop at all unless there's a reason you're doing that which is why I'm asking.
     
  7. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    You could create a mini data class and loop through them that way:
    Code (csharp):
    1. [System.Serializable]
    2. public class UnitStack
    3. {
    4.     public GameObject spawnPrefab;
    5.     public int number;
    6. }
    7.  
    8. public class StacksSpawner : MonoBehaviour
    9. {
    10.     public UnitStack[] unitStacks;
    11.  
    12.     public void Start()
    13.     {
    14.         Vector3 playerSpawnPosition = Vector3.zero; // Don't know how you populate this, so I'll just start at zero
    15.         for(int i = 0; i < unitStacks.length; i++)
    16.         {
    17.             UnitStack currentStack = unitStacks[i]; // Just for clarity and control, could use foreach instead if you want
    18.             for(int j = 0; j < currentStack.number; j++)
    19.             {
    20.                 Instantiate(currentStack.spawnPrefab, playerSpawnPosition, Quaternion.identity);
    21.                 playerSpawnPosition -= new Vector3(0, 1);
    22.             }
    23.            
    24.         }
    25.     }
    26. }