Search Unity

Assigning Multiple Game Objects to an Array in one line of code, Need Help

Discussion in 'Scripting' started by SimpleGiant, Apr 20, 2015.

  1. SimpleGiant

    SimpleGiant

    Joined:
    Dec 22, 2013
    Posts:
    47
    I am making a TD game like Baloons Tower Defence. I am trying to make a list of creeps that will spawn in each wave, but steam line the code as much as posible. I want to assign 10 game objects to a array with one line of code. it seems that i just cant word it properly to get it done.

    this is my hole chink so far (a lot is missing, i am reprogramming the game manager. but you can see what i am needing.)


    The problem is here, can anyone make this work for me?
    Code (CSharp):
    1.             wavePlaner[] = {minion1[0],minion1[0],minion1[0],minion1[0],minion1[0],minion1[0],minion1[0],minion1[0],minion1[0],minion1[0]} as GameObject ;
     
    Last edited: Apr 20, 2015
  2. SimpleGiant

    SimpleGiant

    Joined:
    Dec 22, 2013
    Posts:
    47
    this is the entire class, if you are wondering.
    Code (CSharp):
    1. [code=CSharp]using System.Collections;
    2. using System;
    3.  
    4. [Serializable]
    5.  
    6. public class gameManager : MonoBehaviour {
    7. //Level Settings
    8.     //Notes:easy, med, hard. Any powerups, ect.
    9.  
    10. //Player Settings
    11.     //Notes:sound settings, controles, ect.
    12.  
    13. //Level Variables
    14.     //Notes: Curent Wave, wave total, money, health, towers, minions, ect.
    15.     //Wave Variables.
    16.     public int totalWaveNumber;
    17.     public int currentWaveNumber;
    18.     public int spawnCounter;
    19.     private GameObject[] wavePlaner;
    20.         //This is the spawn timer, to make the creeps trickle out.
    21.     private float timer;
    22.     private float spawnSpeed;
    23.  
    24.     //player variables.
    25.     public int credits;
    26.     public int health;
    27.  
    28.     //minion variables.
    29.     public GameObject[] minion1;
    30.         //minion needed information variables.
    31.     public Transform spawnLocation;
    32.     public Transform[] pathArray;
    33.  
    34.     //tower variables
    35.  
    36.     //tower needed information varibales
    37.     //Notes: powerups and info.
    38.  
    39.     //level settings
    40.     public bool spawningMinions;
    41.  
    42.  
    43.     public GameObject[] Waves(){
    44.         wavePlaner = new GameObject[0];
    45.         //this switch will take the current have number and provide the spawner with a list of minions to spawn, well at the same time adjusting settings acordingly.
    46.         switch(currentWaveNumber){
    47.         case 1:
    48.             wavePlaner[] = {minion1[0],minion1[0],minion1[0],minion1[0],minion1[0],minion1[0],minion1[0],minion1[0],minion1[0],minion1[0]} as GameObject ;
    49.  
    50.             break;
    51.         case 2:
    52.  
    53.             break;
    54.         case 3:
    55.             break;
    56.         case 4:
    57.             break;
    58.         case 5:
    59.             break;
    60.         case 6:
    61.             break;
    62.         case 7:
    63.             break;
    64.         case 8:
    65.             break;
    66.         case 9:
    67.             break;
    68.         case 10:
    69.             break;
    70.         }
    71.     }
    72.  
    [/code]
     
  3. ThomasCreate

    ThomasCreate

    Joined:
    Feb 23, 2015
    Posts:
    81
    What's that "as GameObject" doing there?
     
  4. DuffyT

    DuffyT

    Joined:
    Dec 16, 2014
    Posts:
    37
    Use a for-loop? Something like this:

    Code (CSharp):
    1. public numSpawns;
    2.  
    3. for(int i = 0; i < numSpawns; i++)
    4. {
    5.       wavePlaner[i] = new minion; // whatever gameobjects your enemy is
    6. }
    This is pseudocode, can't type that good from a phone :p
     
  5. SimpleGiant

    SimpleGiant

    Joined:
    Dec 22, 2013
    Posts:
    47
    I was trying to make it work and added it to the end at some point.

    this is possible. ill try using a couple loops and show you what i got.
     
  6. ThomasCreate

    ThomasCreate

    Joined:
    Feb 23, 2015
    Posts:
    81
    Oh, I totally read over the error: it should be like this:

    Code (CSharp):
    1. wavePlaner = new GameObject[] {minion1[0],minion1[0],minion1[0],minion1[0],minion1[0],minion1[0],minion1[0],minion1[0],minion1[0],minion1[0]};
     
    Last edited: Apr 20, 2015
  7. SimpleGiant

    SimpleGiant

    Joined:
    Dec 22, 2013
    Posts:
    47
    thank you my friend.
     
  8. SimpleGiant

    SimpleGiant

    Joined:
    Dec 22, 2013
    Posts:
    47
    i ended up going down a different
    Code (CSharp):
    1. void Waves(){
    2.         //twe are resetting this variable to insure that each time waves is ran, we are asigning to the begining of the array.
    3.         minionArrayPlacekeeper = 0;
    4.  
    5.         if(currentWaveNumber > totalWaveNumber){
    6.             spawningMinions = false;
    7.             Debug.Log("Game Over"); // temp
    8.             return;
    9.         }
    10.  
    11.     //this switch will take the current have number and provide the spawner with a list of minions to spawn, well at the same time adjusting settings acordingly.
    12.         switch(currentWaveNumber){
    13.         case 1:
    14.             incomingWave = new GameObject[6];
    15.             spawnSpeed = 1;
    16.             HaveAsigned(2,minion1[0]);
    17.             HaveAsigned(2,minion1[1]);
    18.             HaveAsigned(2,minion1[0]);
    19.             minionArrayPlacekeeper = 0; //this needs to be set to 0 to make sure the next time Hase Asigned is called, it works properly.
    20.  
    21.             break;
    22.         case 2:
    23.             incomingWave = new GameObject[6];
    24.             spawnSpeed = 1;
    25.             HaveAsigned(2,minion1[0]);
    26.             HaveAsigned(3,minion1[1]);
    27.             HaveAsigned(1,minion1[0]);
    28.             minionArrayPlacekeeper = 0; //this needs to be set to 0 to make sure the next time Hase Asigned is called, it works properly.
    29.  
    30.             break;
    31.         case 3:
    32.             break;
    33.         case 4:
    34.             break;
    35.         case 5:
    36.             break;
    37.         case 6:
    38.             break;
    39.         case 7:
    40.             break;
    41.         case 8:
    42.             break;
    43.         case 9:
    44.             break;
    45.         case 10:
    46.             break;
    47.         }
    48.     }
    49.  
    50.     //this function is a tool, to assign multiple minion gameobjects to the wave at one time.
    51.     void HaveAsigned(int numberOfSpawns, GameObject minionToSpawn){
    52.         //asigning the incoming minions acording to the information given.
    53.         for(int i = 0; i < numberOfSpawns; i++){
    54.             //asign the wave.
    55.             incomingWave[minionArrayPlacekeeper] = minionToSpawn;
    56.             //we are using this variable insted of i, so that way we can run this function again and add more minions of a difrent type to the wave.
    57.             minionArrayPlacekeeper ++;
    58.  
    59.         }
    60.     }
    rout for my waves, so that way i can have each wave change settings like spawn speed.

    This is what i got, what you think?