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

Enemies spawn system near the player

Discussion in 'Scripting' started by xtomyserrax, Jan 14, 2016.

  1. xtomyserrax

    xtomyserrax

    Joined:
    Nov 16, 2015
    Posts:
    75
    Hi guys! How are you?

    Well, i am here to ask you something i need for a game i am making but i dont know how (I have searched in the internet but didnt find what i wanted, if you do please send me the link!)...
    In my game, i have a player and this player has its enemies. The enemies need to be spawned and i helped myself with the Survival Shooter tutorial and i am using that spawn system. I can say that the system works perfectly, but the only thing i have problem is that enemies will spawn in a certain location i set, and not near the player.
    What i want? I want enemies to spawn, but near the player is. Why? Because maybe, the player is very far away from where the spawn point or the spawn points are, and that will get him bored.

    How can i make that kind of system? Of course, the other problem is that we have walls... So should be an override system too that wont make the enemy to spawn into a building, or in a wall or anything bugged.
    This is my code:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class EnemyManager : MonoBehaviour
    4. {
    5.     public PlayerHealth playerHealth;       // Reference to the player's heatlh.
    6.     public GameObject enemy;                // The enemy prefab to be spawned.
    7.     public float spawnTime = 3f;            // How long between each spawn.
    8.     public Transform[] spawnPoints;         // An array of the spawn points this enemy can spawn from.
    9.  
    10.  
    11.     void Start ()
    12.     {
    13.         // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
    14.         InvokeRepeating("Spawn", spawnTime, spawnTime);
    15.     }
    16.  
    17.  
    18.     void Spawn ()
    19.     {
    20.         // If the player has no health left...
    21.         if (playerHealth.currentHealth <= 0f)
    22.         {
    23.             // ... exit the function.
    24.             return;
    25.         }
    26.  
    27.         // Find a random index between zero and one less than the number of spawn points.
    28.         int spawnPointIndex = Random.Range(0, spawnPoints.Length);
    29.  
    30.         // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
    31.         Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
    32.     }
    33. }

    Does anyone know how to make this thing or now a system similar? Could anyone help me?
    PD: Sorry if i have grammar mistakes.
    Well, i wait for your answer! Thank you very much for reading and for your help!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    You can simply do a distance check (and, to account for walls, a raycast) between the spawn point and the player, and re-select one until you find one far away.

    Code (csharp):
    1.  
    2. int emergencyLoopBreaker = 999;
    3. while (emergencyLoopBreaker > 0) {
    4. int spawnPointIndex = Random.Range(0, spawnPoints.Length);
    5. if (Vector3.Distance(spawnPoints[spawnPointIndex].position, thePlayerTransform.position) > someDistance) {
    6. break;
    7. }
    8. emergencyLoopBreaker--;
    9. }
     
    Loud_Lasagna likes this.
  3. xtomyserrax

    xtomyserrax

    Joined:
    Nov 16, 2015
    Posts:
    75
    And how can i make the spawn point to follow the player? I simply add it to the character controller?

    Thank you very much for your help! :D
     
  4. AndreCabral

    AndreCabral

    Joined:
    Jan 11, 2016
    Posts:
    15
    There are many ways to make the spawn point follow the player.

    One simple solution is to make the object that spawn the enemiers a children object to the character object. That way, the spawn object transform will always walk with the player.
     
  5. xtomyserrax

    xtomyserrax

    Joined:
    Nov 16, 2015
    Posts:
    75
    Thank you very much! I will test and tell you how it works for me :D

    If someones knows another alternative or thinks of a way of improving i am full of ears :)
     
  6. xtomyserrax

    xtomyserrax

    Joined:
    Nov 16, 2015
    Posts:
    75
    Could you please explain me how this code work? Sorry i didnt mention i am begginner. Thank you very much!
     
  7. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Rather than use a code which does up to 1000 randoms, lets look at some code that uses spawn points.

    So in your scene put a bunch of spawn points, and attach them to this object and make them all inactive.

    Now, use some code to handle the spawning. Remember to use System.Collections.Generic. It will give you access to a loadable list.

    Code (csharp):
    1.  
    2. using System.Collections.Generic;
    3.  
    4. // in your spawn method....
    5.  
    6.         List<Transform> near = new List<Transform>();
    7.         foreach (Transform spawnPoint in transform.childCount) {
    8.             if (Vector3.Distance(player.transform.position, spawnPoint.position) < someDistance)
    9.             {
    10.                 near.Add(spawnPoint);
    11.             }
    12.  
    13.             if (near.Count > 0) {
    14.                 int index = Random.Range(0, spawnPoint.Count);
    15.                 Transform spawnAt = spawnPoints[index];
    16.                 if (spawnAt != null) {
    17.                     GameObject instance = Instantiate(objectToSpawn, spawnAt.position, Quaternion.identity);
    18.                     instance.transform.Rotate(Vector3.up, Random.Range(0f, 360f));
    19.                 }
    20.             }
    21.         }
    22.  
    Now, this code simply finds all of the points near the player and lists them. Now, if we have something in the list, we can just randomly pick one, and spawn it there.
     
  8. xtomyserrax

    xtomyserrax

    Joined:
    Nov 16, 2015
    Posts:
    75
    Wow thats great so: As i have a big world, I make a lot of spawn points, and the system will choose the most nearly? Do I have to set a radius? I like this system, it is great!
    What should i put in this "someDistance"?

    Sorry i am not expert, i know how to code easy things, jeje.

    Thank you very much for your help :D
     
  9. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    perhaps...

    Code (csharp):
    1. public float spawnDistance = 30;
     
  10. xtomyserrax

    xtomyserrax

    Joined:
    Nov 16, 2015
    Posts:
    75
    Thanks for your help, now i am having this code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class EnemyManager : MonoBehaviour
    5. {
    6.     public PlayerHealth playerHealth;       // Reference to the player's heatlh.
    7.     public Transform player;
    8.     public GameObject enemy;                // The enemy prefab to be spawned.
    9.     public float spawnTime = 3f;            // How long between each spawn.
    10.     public float spawnDistance = 30;
    11.     public Transform[] spawnPoints;         // An array of the spawn points this enemy can spawn from.
    12.  
    13.     void Start()
    14.     {
    15.         // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
    16.         InvokeRepeating("Spawn", spawnTime, spawnTime);
    17.     }
    18.  
    19. void Spawn ()
    20.     {
    21.         // If the player has no health left...
    22.         if (playerHealth.currentHealth <= 0f)
    23.         {
    24.             // ... exit the function.
    25.             return;
    26.         }
    27.  
    28.         List<Transform> near = new List<Transform>();
    29.         foreach (Transform spawnPoint in transform.childCount)
    30.         {
    31.             if (Vector3.Distance(player.transform.position, spawnPoint.position) < spawnDistance)
    32.             {
    33.                 near.Add(spawnPoint);
    34.             }
    35.  
    36.             if (near.Count > 0)
    37.             {
    38.                 int index = Random.Range(0, spawnPoint.Count);
    39.                 Transform spawnAt = spawnPoints[index];
    40.                 if (spawnAt != null)
    41.                 {
    42.                     GameObject instance = Instantiate(objectToSpawn, spawnAt.position, Quaternion.identity);
    43.                     instance.transform.Rotate(Vector3.up, Random.Range(0f, 360f));
    44.                 }
    45.             }
    46.         }
    47.  
    48.         // Find a random index between zero and one less than the number of spawn points.
    49.         int spawnPointIndex = Random.Range(0, spawnPoints.Length);
    50.  
    51.         // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
    52.         Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
    53.     }
    54. }
    55.  

    And i am having this errors:




    Could you help me telling me what should i do to make them work? Thank you and sorry for being annoying! You are helping me a lot!
     
  11. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    change
    Code (csharp):
    1. foreach (Transform spawnPoint in transform.childCount)
    2. {
    to

    Code (csharp):
    1. for(int i=0; i< transform.childCount; i++)
    2. {
    3. Transform spawnPoint = transform.GetChild(i);
    The second one should be spawnPoints....

    The third one, you need to add a variable at the top...

    Code (csharp):
    1. public GameObject objectToSpawn;
    and fill it with something to spawn.
     
  12. xtomyserrax

    xtomyserrax

    Joined:
    Nov 16, 2015
    Posts:
    75
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class EnemyManager : MonoBehaviour
    5. {
    6.     public PlayerHealth playerHealth;       // Reference to the player's heatlh.
    7.     public Transform player;            // The position that that camera will be following.
    8.     public GameObject enemy;                // The enemy prefab to be spawned.
    9.     public float spawnTime = 3f;            // How long between each spawn.
    10.     public float spawnDistance = 30;
    11.     public Transform[] spawnPoints;         // An array of the spawn points this enemy can spawn from.
    12.  
    13.     void Start()
    14.     {
    15.         // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
    16.         InvokeRepeating("Spawn", spawnTime, spawnTime);
    17.     }
    18.  
    19. void Spawn ()
    20.     {
    21.         // If the player has no health left...
    22.         if (playerHealth.currentHealth <= 0f)
    23.         {
    24.             // ... exit the function.
    25.             return;
    26.         }
    27.  
    28.         List<Transform> near = new List<Transform>();
    29.         for (int i = 0; i < transform.childCount; i++)
    30.         {
    31.             Transform spawnPoint = transform.GetChild(i);
    32.             if (Vector3.Distance(player.transform.position, spawnPoint.position) < spawnDistance)
    33.             {
    34.                 near.Add(spawnPoint);
    35.             }
    36.  
    37.             if (near.Count > 0)
    38.             {
    39.                 int index = Random.Range(0, spawnPoints.Count);
    40.                 Transform spawnAt = spawnPoints[index];
    41.                 if (spawnAt != null)
    42.                 {
    43.                     GameObject instance = Instantiate(enemy, spawnAt.position, Quaternion.identity);
    44.                     instance.transform.Rotate(Vector3.up, Random.Range(0f, 360f));
    45.                 }
    46.             }
    47.         }
    48.  
    49.         // Find a random index between zero and one less than the number of spawn points.
    50.         int spawnPointIndex = Random.Range(0, spawnPoints.Length);
    51.  
    52.         // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
    53.         Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
    54.     }
    55. }

    Keep with problems here:

    Code (CSharp):
    1.                 int index = Random.Range(0, spawnPoints.Count);
    With the world ".Count" i keep having this error:
    Error CS1061 'Transform[]' does not contain a definition for 'Count' and no extension method 'Count' accepting a first argument of type 'Transform[]' could be found (are you missing a using directive or an assembly reference?)

    Code (CSharp):
    1.                     GameObject instance = Instantiate(objectToSpawn, spawnAt.position, Quaternion.identity);
    With this: "Instantiate(objectToSpawn, spawnAt.position, Quaternion.identity);" I am now having this error:
    Error CS0266 Cannot implicitly convert type 'UnityEngine.Object' to 'UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)

    Thank you and sorry!
     
    Last edited: Jan 16, 2016
  13. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Sorry, Transform[] is an array. the [] denotes that it is an array. My reference was to a List<Transform> The list object is a System.Collections.Generic object.

    Arrays have .Length. Lists have .Count. So change it to .Length.

    Also, as you are typing in VS or Mono, if you press a period after a variable, you will see properties and methods of that variable. If you hover over any of them, it should explain what they are.
     
  14. xtomyserrax

    xtomyserrax

    Joined:
    Nov 16, 2015
    Posts:
    75
    Hey thank you! I am feeling like a pain in the ass for you. Sorry for being annoying but i keep having the second error:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class EnemyManager : MonoBehaviour
    5. {
    6.     public PlayerHealth playerHealth;       // Reference to the player's heatlh.
    7.     public Transform player;            // The position that that camera will be following.
    8.     public GameObject enemy;                // The enemy prefab to be spawned.
    9.     public float spawnTime = 3f;            // How long between each spawn.
    10.     public float spawnDistance = 30;
    11.     public Transform[] spawnPoints;         // An array of the spawn points this enemy can spawn from.
    12.  
    13.     void Start()
    14.     {
    15.         // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
    16.         InvokeRepeating("Spawn", spawnTime, spawnTime);
    17.     }
    18.  
    19. void Spawn ()
    20.     {
    21.         // If the player has no health left...
    22.         if (playerHealth.currentHealth <= 0f)
    23.         {
    24.             // ... exit the function.
    25.             return;
    26.         }
    27.  
    28.         List<Transform> near = new List<Transform>();
    29.         for (int i = 0; i < transform.childCount; i++)
    30.         {
    31.             Transform spawnPoint = transform.GetChild(i);
    32.             if (Vector3.Distance(player.transform.position, spawnPoint.position) < spawnDistance)
    33.             {
    34.                 near.Add(spawnPoint);
    35.             }
    36.  
    37.             if (near.Count > 0)
    38.             {
    39.                 int index = Random.Range(0, spawnPoints.Length);
    40.                 Transform spawnAt = spawnPoints[index];
    41.                 if (spawnAt != null)
    42.                 {
    43.                     GameObject instance = Instantiate(enemy, spawnAt.position, Quaternion.identity);
    44.                     instance.transform.Rotate(Vector3.up, Random.Range(0f, 360f));
    45.                 }
    46.             }
    47.         }
    48.  
    49.         // Find a random index between zero and one less than the number of spawn points.
    50.         int spawnPointIndex = Random.Range(0, spawnPoints.Length);
    51.  
    52.         // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
    53.         Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
    54.     }
    55. }
    56.  

    And this is the error:
    Code (CSharp):
    1.  GameObject instance = Instantiate(enemy, spawnAt.position, Quaternion.identity);
    With this part of the line: "Instantiate(enemy, spawnAt.position, Quaternion.identity);" I am now having this error: Error CS0266 Cannot implicitly convert type 'UnityEngine.Object' to 'UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)

    In the internet i have found that Instantiate returns an Object, so thats why i get this error.
    Code (CSharp):
    1.                     GameObject instance = Instantiate(enemy, spawnAt.position, Quaternion.identity) as GameObject;
    Is that "fix code" fine?

    Thank you very much!
     
  15. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    You need to typecast the result from Instantiate:
    Code (csharp):
    1. GameObject foo = (GameObject)Instantiate(stuff);
     
  16. xtomyserrax

    xtomyserrax

    Joined:
    Nov 16, 2015
    Posts:
    75
    The system is working! Thank you both for the help you gave, now i am having this inconvenient:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class EnemyManager : MonoBehaviour
    5. {
    6.     public PlayerHealth playerHealth;       // Reference to the player's heatlh.
    7.     public Transform player;            // The position that that camera will be following.
    8.     public GameObject enemy;                // The enemy prefab to be spawned.
    9.     public float spawnTime = 3f;            // How long between each spawn.
    10.     public float spawnDistance = 10;
    11.     public Transform[] spawnPoints;         // An array of the spawn points this enemy can spawn from.
    12.  
    13.     void Start()
    14.     {
    15.         // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
    16.         InvokeRepeating("Spawn", spawnTime, spawnTime);
    17.     }
    18.  
    19. void Spawn ()
    20.     {
    21.         // If the player has no health left...
    22.         if (playerHealth.currentHealth <= 0f)
    23.         {
    24.             // ... exit the function.
    25.             return;
    26.         }
    27.  
    28.         List<Transform> near = new List<Transform>();
    29.         for (int i = 0; i < transform.childCount; i++)
    30.         {
    31.             Transform spawnPoint = transform.GetChild(i);
    32.             if (Vector3.Distance(player.transform.position, spawnPoint.position) <= spawnDistance)
    33.             {
    34.                 near.Add(spawnPoint);
    35.             }
    36.  
    37.             if (Vector3.Distance(player.transform.position, spawnPoint.position) > spawnDistance)
    38.             {
    39.                 near.Remove(spawnPoint);
    40.             }
    41.  
    42.             if (near.Count > 0)
    43.             {
    44.                 int index = Random.Range(0, spawnPoints.Length);
    45.                 Transform spawnAt = spawnPoints[index];
    46.                 if (spawnAt != null)
    47.                 {
    48.                     //GameObject instance = Instantiate(enemy, spawnAt.position, Quaternion.identity) as GameObject;
    49.                     GameObject instance = (GameObject)Instantiate(enemy, spawnAt.position, Quaternion.identity);
    50.                     instance.transform.Rotate(Vector3.up, Random.Range(0f, 360f));
    51.                 }
    52.             }
    53.         }
    54.  
    55.         // Find a random index between zero and one less than the number of spawn points.
    56.         //int spawnPointIndex = Random.Range(0, spawnPoints.Length);
    57.  
    58.         // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
    59.         //Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
    60.     }
    61. }
    62.  

    I am using a small distance of 10 to test and i have added a remove for the list so if the player is far, the spawn will get removed.

    Whats my problem?
    First i dont know why i am having 2 or 3 enemies spawned per time (Maybe because i have 3 spawn points, and it choose the spawn and spawn the 3).
    Second: I have my player far away from a spawn point. This player is in the same X of the spawn point but like 100 blocks far away from the Z. The enemies spawns too there, i think that maybe the player is in the same X. How can we change this? (I have tested that when the player is in the same Z of the Spawn point, this doesnt happen, the spawn waits the player to be closer.)

    Thank you very much for all the help you are giving me!
     
  17. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    ok, your getting mixed up here.

    The concept of the code that I gave was that you create a list.... and add things to it that are close. If an object is not close, you don't add it to it. So adding the .Remove() to it does nothing but waste processing power.

    Next, as I am looking at your code, you are going through each child of this object and then creating a random for the child. The concept I wished for you was to use each child of this object as a spawn point. What you have ignores the children, and simply picks a random value from a variable called spawnPoints. Thus supplanting anything that was done in the script.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class Test : MonoBehaviour
    6. {
    7.  
    8.     public PlayerHealth playerHealth;       // Reference to the player's heatlh.
    9.     public Transform player;            // The position that that camera will be following.
    10.     public GameObject enemy;                // The enemy prefab to be spawned.
    11.     public float spawnTime = 3f;            // How long between each spawn.
    12.     public float spawnDistance = 10;
    13.  
    14.     void Start()
    15.     {
    16.         for (int i = 0; i < transform.childCount; i++) {
    17.             transform.GetChild(i).gameObject.SetActive(false);
    18.         }
    19.         // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
    20.         InvokeRepeating("Spawn", Time.time, spawnTime);
    21.     }
    22.  
    23.     void Spawn()
    24.     {
    25.         // If the player has no health left...
    26.         //if (playerHealth.currentHealth <= 0f)
    27.         {
    28.             // ... exit the function.
    29.             return;
    30.         }
    31.  
    32.         // collect the children that are close.
    33.         List<Transform> near = new List<Transform>();
    34.         for (int i = 0; i < transform.childCount; i++)
    35.         {
    36.             Transform spawnPoint = transform.GetChild(i);
    37.             if (Vector3.Distance(player.transform.position, spawnPoint.position) <= spawnDistance)
    38.             {
    39.                 near.Add(spawnPoint);
    40.             }
    41.         }
    42.  
    43.         if (near.Count > 0) {
    44.             // Find a random index between zero and one less than the number of spawn points.
    45.             int spawnPointIndex = Random.Range(0, near.Count);
    46.  
    47.             // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
    48.             GameObject instance = (GameObject)Instantiate(enemy, near[spawnPointIndex].position, near[spawnPointIndex].rotation);
    49.             instance.transform.Rotate(Vector3.up, Random.Range(0f, 360f));
    50.         }
    51.     }
    52. }
    53.  
     
  18. xtomyserrax

    xtomyserrax

    Joined:
    Nov 16, 2015
    Posts:
    75
    Hey thanks! It is working really well now! Do you have any recommendation to improve it or it is well like that?

    If you have or someone else, please tell me!

    Thank you for all the help you gave me and the time. If I have any problem ill tell you, and sorry for my lack of knowledge! I am trying to learn :D
     
  19. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Learning is what this community forum is for. ;)


    Addition:
    You could change "void Spawn()" to void Spawn(int count = 1)" Then in your code, select count points. You do this by maintaining another list. You get a random integer from the number of nearest points, then add that point to the new list. Once that is done, remove it from the old list. Since you have the integer near.RemoveAt(spawnPointIndex ); would work.

    Now, you foreach through the new list and spawn that many times.

    The use would be on Start, you want to spawn 10 enemies in the scene. Since this one only spawns 1 every 3 seconds, 10 at a time would not work. Calling this 10 times, may put enemies in the same spot as one another.
     
  20. xtomyserrax

    xtomyserrax

    Joined:
    Nov 16, 2015
    Posts:
    75
    Sorry but i couldnt understand your idea, Could you write it in the code please?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class EnemyManager : MonoBehaviour
    5. {
    6.     public PlayerHealth playerHealth;       // Reference to the player's heatlh.
    7.     public Transform player;            // The position that that camera will be following.
    8.     public GameObject enemy;                // The enemy prefab to be spawned.
    9.     public float spawnTime = 3f;            // How long between each spawn.
    10.     public float spawnDistance = 10;
    11.     public Transform[] spawnPoints;         // An array of the spawn points this enemy can spawn from.
    12.  
    13.     void Start()
    14.     {
    15.         // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
    16.         InvokeRepeating("Spawn", spawnTime, spawnTime);
    17.     }
    18.  
    19. void Spawn ()
    20.     {
    21.         // If the player has no health left...
    22.         if (playerHealth.currentHealth <= 0f)
    23.         {
    24.             // ... exit the function.
    25.             return;
    26.         }
    27.  
    28.         // collect the children that are close.
    29.         List<Transform> near = new List<Transform>();
    30.         for (int i = 0; i < transform.childCount; i++)
    31.         {
    32.             Transform spawnPoint = transform.GetChild(i);
    33.             if (Vector3.Distance(player.transform.position, spawnPoint.position) <= spawnDistance)
    34.             {
    35.                 near.Add(spawnPoint);
    36.             }
    37.         }
    38.  
    39.         if (near.Count > 0)
    40.         {
    41.             // Find a random index between zero and one less than the number of spawn points.
    42.             int spawnPointIndex = Random.Range(0, near.Count);
    43.  
    44.             // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
    45.             GameObject instance = (GameObject)Instantiate(enemy, near[spawnPointIndex].position, near[spawnPointIndex].rotation);
    46.             instance.transform.Rotate(Vector3.up, Random.Range(0f, 360f));
    47.         }
    48.     }
    49. }
    50.  
    Thank you and thank you and thank you very much!
     
  21. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    I hate to be insistent on this; I need for you to try this. Let me break it out.

    We want Spawn to be able to be sent a value. By using "int count = 1", we default that value to 1, and make it where it is not required to be sent to it.

    Once we have figured out that we have something near us, we need to create another list.

    loop from 0 to count and get an index randomly from the nearest ones we have. (as long as we have something in the list to pick from)

    add the near spawn point for that index to the new list, and remove it from near.

    for each of those spawn points, instantiate an enemy.
     
  22. xtomyserrax

    xtomyserrax

    Joined:
    Nov 16, 2015
    Posts:
    75
    Sorry, i have tried but i cant. I mean, i understand your idea, but i dont know how to implement it. I tried adding things but they have no sense and are incorrect.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class EnemyManager : MonoBehaviour
    5. {
    6.     public PlayerHealth playerHealth;       // Reference to the player's heatlh.
    7.     public Transform player;            // The position that that camera will be following.
    8.     public GameObject enemy;                // The enemy prefab to be spawned.
    9.     public float spawnTime = 3f;            // How long between each spawn.
    10.     public float spawnDistance = 10;
    11.     public Transform[] spawnPoints;         // An array of the spawn points this enemy can spawn from.
    12.  
    13.     void Start()
    14.     {
    15.         // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
    16.         InvokeRepeating("Spawn", spawnTime, spawnTime);
    17.     }
    18.  
    19. void Spawn(int count = 1)
    20.     {
    21.         // If the player has no health left...
    22.         if (playerHealth.currentHealth <= 0f)
    23.         {
    24.             // ... exit the function.
    25.             return;
    26.         }
    27.  
    28.         // collect the children that are close.
    29.         List<Transform> near = new List<Transform>();
    30.         for (int i = 0; i < transform.childCount; i++)
    31.         {
    32.             Transform spawnPoint = transform.GetChild(i);
    33.             if (Vector3.Distance(player.transform.position, spawnPoint.position) <= spawnDistance)
    34.             {
    35.                 near.Add(spawnPoint);
    36.             }
    37.         }
    38.  
    39.         List<Transform> nearofnear = new List<Transform>();
    40.         for (List<Transform> nearadsf; i < transform.childCount; i++)
    41.         {
    42.  
    43.             near.Contains<>;
    44.             Transform spawnPoint = transform.GetChild(i);
    45.             if (Vector3.Distance(player.transform.position, near(spawnPoint).position) <= spawnDistance)
    46.             {
    47.                 near.Add(spawnPoint);
    48.             }
    49.         }
    50.  
    51.         if (near.Count > 0)
    52.         {
    53.             // Find a random index between zero and one less than the number of spawn points.
    54.             int spawnPointIndex = Random.Range(0, near.Count);
    55.  
    56.             // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
    57.             GameObject instance = (GameObject)Instantiate(enemy, near[spawnPointIndex].position, near[spawnPointIndex].rotation);
    58.             instance.transform.Rotate(Vector3.up, Random.Range(0f, 360f));
    59.         }
    60.     }
    61. }
    Thanks and sorry
     
  23. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    ok... I am doing this so you can learn... ;)

    Code (csharp):
    1.  
    2.             if (near.Count > 0)
    3.             {
    4.                 // create a new list here.... same as line 29, but call the variable something else.
    5.  
    6.                 // loop from 0 to <count here..... same as line 30 in the code you just had. {
    7.                 // make sure you include the below lines....
    8.                 // Find a random index between zero and one less than the number of spawn points.
    9.                 int spawnPointIndex = Random.Range(0, near.Count);
    10.                 // add near[spawnPointIndex] to the new list // same as line 35
    11.                 // remove spawnPointIndex from near // new line: near.RemoveAt(spawnPointIndex);
    12.                 // if we dont have any more points. break... // new line: Break;
    13.                 // end the loop... }
    14.  
    15.                 // loop through each of your new variable.... new line: foreach(Transform spawnPoint in newVariableName){
    16.                 // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
    17.                 GameObject instance = (GameObject)Instantiate(enemy, spawnPoint.position, spawnPoint.rotation);
    18.                 instance.transform.Rotate(Vector3.up, Random.Range(0f, 360f));
    19.                 // end the loop.... }
    20.             }
    21.  
    Please read it carefully, some of the new code is in there.
     
  24. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Code (csharp):
    1.  
    2.             if (near.Count > 0)
    3.             {
    4.                 List<Transform> pointsToSpawn = new List<Transform>();
    5.  
    6.                 for(int i=0; i<count; i++){
    7.                     // Find a random index between zero and one less than the number of spawn points.
    8.                     int spawnPointIndex = Random.Range(0, near.Count);
    9.                     pointsToSpawn.Add(near[spawnPointIndex]);
    10.                     near.RemoveAt(spawnPointIndex);
    11.                     if(near.Count == 0) break;
    12.                 }
    13.  
    14.                 foreach(Transform spawnPoint in pointsToSpawn){
    15.                     // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
    16.                     GameObject instance = (GameObject)Instantiate(enemy, spawnPoint.position, spawnPoint.rotation);
    17.                     instance.transform.Rotate(Vector3.up, Random.Range(0f, 360f));
    18.                 }
    19.             }
     
  25. xtomyserrax

    xtomyserrax

    Joined:
    Nov 16, 2015
    Posts:
    75
    With the help you gave me i could make this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class EnemyManager : MonoBehaviour
    5. {
    6.     public PlayerHealth playerHealth;       // Reference to the player's heatlh.
    7.     public Transform player;            // The position that that camera will be following.
    8.     public GameObject enemy;                // The enemy prefab to be spawned.
    9.     public float spawnTime = 3f;            // How long between each spawn.
    10.     public float spawnDistance = 10;
    11.     public float spawnscore = 100f;
    12.     public float maxspawnscore = 100f;
    13.     public Transform[] spawnPoints;         // An array of the spawn points this enemy can spawn from.
    14.  
    15.     void Start()
    16.     {
    17.         // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
    18.         InvokeRepeating("Spawn", spawnTime, spawnTime);
    19.     }
    20.  
    21.     void Spawn()
    22.     {
    23.         // If the player has no health left...
    24.         if (playerHealth.currentHealth <= 0f)
    25.         {
    26.             // ... exit the function.
    27.             return;
    28.         }
    29.  
    30.         if (ScoreManager.score < spawnscore)
    31.         {
    32.             return;
    33.         }
    34.  
    35.         if (ScoreManager.score >= maxspawnscore)
    36.         {
    37.             return;
    38.         }
    39.  
    40.         // collect the children that are close.
    41.         List<Transform> near = new List<Transform>();
    42.         for (int i = 0; i < transform.childCount; i++)
    43.         {
    44.             Transform spawnPoint = transform.GetChild(i);
    45.             if (Vector3.Distance(player.transform.position, spawnPoint.position) <= spawnDistance)
    46.             {
    47.                 near.Add(spawnPoint);
    48.             }
    49.         }
    50.  
    51.  
    52.         if (near.Count > 0)
    53.         {
    54.             // create a new list here.... same as line 29, but call the variable something else.
    55.             List<Transform> nearofnear = new List<Transform>();
    56.  
    57.             // loop from 0 to <count here..... same as line 30 in the code you just had. {
    58.             // make sure you include the below lines....
    59.             for (int i = 0; i < transform.childCount; i++)
    60.             {
    61.                 // Find a random index between zero and one less than the number of spawn points.
    62.                 int spawnPointIndex = Random.Range(0, near.Count - 1);
    63.  
    64.                 // add near[spawnPointIndex] to the new list // same as line 35
    65.                 nearofnear.Add(near[spawnPointIndex]);
    66.  
    67.                 // remove spawnPointIndex from near // new line: near.RemoveAt(spawnPointIndex);
    68.                 near.RemoveAt(spawnPointIndex);
    69.  
    70.                 // if we dont have any more points. break... // new line: Break;
    71.                 if (near.Count < 0)
    72.                 {
    73.                     Break; // ERROR: Error CS0103  The name 'Break' does not exist in the current context. LINE: 61
    74.  
    75.                 }
    76.  
    77.                 // end the loop... }
    78.             }
    79.  
    80.             // loop through each of your new variable.... new line: foreach(Transform spawnPoint in newVariableName){
    81.             foreach (Transform spawnPoint in theSpawnPoint) // ERROR: Error CS0201  Only assignment, call, increment, decrement, and new object expressions can be used as a statement.  LINE: 61
    82.  
    83.             {
    84.                 // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
    85.                 GameObject instance = (GameObject)Instantiate(enemy, spawnPoint.position, spawnPoint.rotation);
    86.                 instance.transform.Rotate(Vector3.up, Random.Range(0f, 360f));
    87.                 // end the loop.... }
    88.             }
    89.         }
    90.     }
    91. }

    There i have added an score manager too but it works. I have two errors, and i see you also sent me the script as it should be so i am going to correct myself with that! Thank you very much! I test and tell you how it works :D

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class EnemyManager : MonoBehaviour
    5. {
    6.     public PlayerHealth playerHealth;       // Reference to the player's heatlh.
    7.     public Transform player;            // The position that that camera will be following.
    8.     public GameObject enemy;                // The enemy prefab to be spawned.
    9.     public float spawnTime = 3f;            // How long between each spawn.
    10.     public float spawnDistance = 10;
    11.     public float spawnscore = 100f;
    12.     public float maxspawnscore = 100f;
    13.     public Transform[] spawnPoints;         // An array of the spawn points this enemy can spawn from.
    14.  
    15.     void Start()
    16.     {
    17.         // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
    18.         InvokeRepeating("Spawn", spawnTime, spawnTime);
    19.     }
    20.  
    21.     void Spawn()
    22.     {
    23.         // If the player has no health left...
    24.         if (playerHealth.currentHealth <= 0f)
    25.         {
    26.             // ... exit the function.
    27.             return;
    28.         }
    29.  
    30.         if (ScoreManager.score < spawnscore)
    31.         {
    32.             return;
    33.         }
    34.  
    35.         if (ScoreManager.score >= maxspawnscore)
    36.         {
    37.             return;
    38.         }
    39.  
    40.         // collect the children that are close.
    41.         List<Transform> near = new List<Transform>();
    42.         for (int i = 0; i < transform.childCount; i++)
    43.         {
    44.             Transform spawnPoint = transform.GetChild(i);
    45.             if (Vector3.Distance(player.transform.position, spawnPoint.position) <= spawnDistance)
    46.             {
    47.                 near.Add(spawnPoint);
    48.             }
    49.         }
    50.  
    51.  
    52.         if (near.Count > 0)
    53.         {
    54.             // create a new list here.... same as line 29, but call the variable something else.
    55.             List<Transform> pointsToSpawn = new List<Transform>();
    56.  
    57.             // loop from 0 to <count here..... same as line 30 in the code you just had. {
    58.             // make sure you include the below lines....
    59.             for (int i = 0; i < count; i++)
    60.             {
    61.                 // Find a random index between zero and one less than the number of spawn points.
    62.                 int spawnPointIndex = Random.Range(0, near.Count);
    63.  
    64.                 // add near[spawnPointIndex] to the new list // same as line 35
    65.                 pointsToSpawn.Add(near[spawnPointIndex]);
    66.  
    67.                 // remove spawnPointIndex from near // new line: near.RemoveAt(spawnPointIndex);
    68.                 near.RemoveAt(spawnPointIndex);
    69.  
    70.                 // if we dont have any more points. break... // new line: Break;
    71.                 if (near.Count == 0) break;
    72.  
    73.                 // end the loop... }
    74.             }
    75.  
    76.             // loop through each of your new variable.... new line: foreach(Transform spawnPoint in newVariableName){
    77.             foreach (Transform spawnPoint in pointsToSpawn)
    78.             {
    79.                 // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
    80.                 GameObject instance = (GameObject)Instantiate(enemy, spawnPoint.position, spawnPoint.rotation);
    81.                 instance.transform.Rotate(Vector3.up, Random.Range(0f, 360f));
    82.             }
    83.         }
    84.     }
    85. }

    I have an error in line 59 with count. (I have tried with Count but doesnt work too)
    Code (CSharp):
    1.             for (int i = 0; i < count; i++)
    The error is: Error CS0103 The name 'count' does not exist in the current context.

    Thank you very much for all your help. You are my hero, :rolleyes:
     
    Last edited: Jan 18, 2016
  26. xtomyserrax

    xtomyserrax

    Joined:
    Nov 16, 2015
    Posts:
    75
    * Message edited *
     
  27. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Man, you almost had it. ;)

    Good job. your task is to understand what that stuff does though.
     
  28. xtomyserrax

    xtomyserrax

    Joined:
    Nov 16, 2015
    Posts:
    75
    Haha, you did it but well, with experience we learn so with time ill do. I am happy because i could create my own script without help (not this one). Of course it was something a little bit "stupid" but i needed it so it was great. jaja.

    Emmm, look i am having this error, what should i do?


    Ty
     
  29. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    what happend to Spawn(int count = 1)???
     
  30. xtomyserrax

    xtomyserrax

    Joined:
    Nov 16, 2015
    Posts:
    75
    Sorry, i accidently deleted it!

    Thank you very much again! I dont know how to thank you :(
     
  31. xtomyserrax

    xtomyserrax

    Joined:
    Nov 16, 2015
    Posts:
    75
    I am getting this message in the console when i start play mode (it is not an error): "Trying to Invoke method: EnemyManager.Spawn couldn't be called."

    And the enemies are not spawning.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class EnemyManager : MonoBehaviour
    5. {
    6.     public PlayerHealth playerHealth;       // Reference to the player's heatlh.
    7.     public Transform player;            // The position that that camera will be following.
    8.     public GameObject enemy;                // The enemy prefab to be spawned.
    9.     public float spawnTime = 3f;            // How long between each spawn.
    10.     public float spawnDistance = 10;
    11.     public float spawnscore = 100f;
    12.     public float maxspawnscore = 100f;
    13.     public Transform[] spawnPoints;         // An array of the spawn points this enemy can spawn from.
    14.  
    15.     void Start()
    16.     {
    17.         // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
    18.         InvokeRepeating("Spawn", spawnTime, spawnTime);
    19.     }
    20.  
    21.     void Spawn(int count = 1)
    22.     {
    23.         // If the player has no health left...
    24.         if (playerHealth.currentHealth <= 0f)
    25.         {
    26.             // ... exit the function.
    27.             return;
    28.         }
    29.  
    30.         if (ScoreManager.score < spawnscore)
    31.         {
    32.             return;
    33.         }
    34.  
    35.         if (ScoreManager.score >= maxspawnscore)
    36.         {
    37.             return;
    38.         }
    39.  
    40.         // collect the children that are close.
    41.         List<Transform> near = new List<Transform>();
    42.         for (int i = 0; i < transform.childCount; i++)
    43.         {
    44.             Transform spawnPoint = transform.GetChild(i);
    45.             if (Vector3.Distance(player.transform.position, spawnPoint.position) <= spawnDistance)
    46.             {
    47.                 near.Add(spawnPoint);
    48.             }
    49.         }
    50.  
    51.  
    52.         if (near.Count > 0)
    53.         {
    54.             // create a new list here.... same as line 29, but call the variable something else.
    55.             List<Transform> pointsToSpawn = new List<Transform>();
    56.  
    57.             // loop from 0 to <count here..... same as line 30 in the code you just had. {
    58.             // make sure you include the below lines....
    59.             for (int i = 0; i < count; i++)
    60.             {
    61.                 // Find a random index between zero and one less than the number of spawn points.
    62.                 int spawnPointIndex = Random.Range(0, near.Count);
    63.  
    64.                 // add near[spawnPointIndex] to the new list // same as line 35
    65.                 pointsToSpawn.Add(near[spawnPointIndex]);
    66.  
    67.                 // remove spawnPointIndex from near // new line: near.RemoveAt(spawnPointIndex);
    68.                 near.RemoveAt(spawnPointIndex);
    69.  
    70.                 // if we dont have any more points. break... // new line: Break;
    71.                 if (near.Count == 0) break;
    72.  
    73.                 // end the loop... }
    74.             }
    75.  
    76.             // loop through each of your new variable.... new line: foreach(Transform spawnPoint in newVariableName){
    77.             foreach (Transform spawnPoint in pointsToSpawn)
    78.             {
    79.                 // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
    80.                 GameObject instance = (GameObject)Instantiate(enemy, spawnPoint.position, spawnPoint.rotation);
    81.                 instance.transform.Rotate(Vector3.up, Random.Range(0f, 360f));
    82.             }
    83.         }
    84.     }
    85. }

    Have an idea? Thanks :D
     
  32. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    make use of the Debug.Log() method.

    Code (csharp):
    1.  
    2. Debug.Log("I reached this point");
    3.  
    4. // or
    5.  
    6. Debug.Log(near.Count);
    7.  
     
  33. xtomyserrax

    xtomyserrax

    Joined:
    Nov 16, 2015
    Posts:
    75
    Hey, i put 5 debug messages in all the code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class EnemyManager : MonoBehaviour
    5. {
    6.     public PlayerHealth playerHealth;       // Reference to the player's heatlh.
    7.     public Transform player;            // The position that that camera will be following.
    8.     public GameObject enemy;                // The enemy prefab to be spawned.
    9.     public float spawnTime = 3f;            // How long between each spawn.
    10.     public float spawnDistance = 10;
    11.     public float spawnscore = 100f;
    12.     public float maxspawnscore = 100f;
    13.     public Transform[] spawnPoints;         // An array of the spawn points this enemy can spawn from.
    14.  
    15.     void Start()
    16.     {
    17.         // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
    18.         InvokeRepeating("Spawn", spawnTime, spawnTime);
    19.         Debug.Log("I reached this point 1");
    20.     }
    21.  
    22.     void Spawn(int count = 1)
    23.     {
    24.         // If the player has no health left...
    25.         if (playerHealth.currentHealth <= 0f)
    26.         {
    27.             // ... exit the function.
    28.             return;
    29.         }
    30.  
    31.         if (ScoreManager.score < spawnscore)
    32.         {
    33.             return;
    34.         }
    35.  
    36.         if (ScoreManager.score >= maxspawnscore)
    37.         {
    38.             return;
    39.         }
    40.  
    41.         // collect the children that are close.
    42.         List<Transform> near = new List<Transform>();
    43.         for (int i = 0; i < transform.childCount; i++)
    44.         {
    45.             Transform spawnPoint = transform.GetChild(i);
    46.             if (Vector3.Distance(player.transform.position, spawnPoint.position) <= spawnDistance)
    47.             {
    48.                 near.Add(spawnPoint);
    49.             }
    50.             Debug.Log("I reached this point 2");
    51.             Debug.Log(near.Count);
    52.         }
    53.  
    54.  
    55.         if (near.Count > 0)
    56.         {
    57.             // create a new list here.... same as line 29, but call the variable something else.
    58.             List<Transform> pointsToSpawn = new List<Transform>();
    59.  
    60.             // loop from 0 to <count here..... same as line 30 in the code you just had. {
    61.             // make sure you include the below lines....
    62.             for (int i = 0; i < count; i++)
    63.             {
    64.                 // Find a random index between zero and one less than the number of spawn points.
    65.                 int spawnPointIndex = Random.Range(0, near.Count);
    66.  
    67.                 // add near[spawnPointIndex] to the new list // same as line 35
    68.                 pointsToSpawn.Add(near[spawnPointIndex]);
    69.  
    70.                 // remove spawnPointIndex from near // new line: near.RemoveAt(spawnPointIndex);
    71.                 near.RemoveAt(spawnPointIndex);
    72.  
    73.                 // if we dont have any more points. break... // new line: Break;
    74.                 if (near.Count == 0) break;
    75.                 Debug.Log("I reached this point 3");
    76.                 Debug.Log(near.Count);
    77.                 // end the loop... }
    78.             }
    79.  
    80.             // loop through each of your new variable.... new line: foreach(Transform spawnPoint in newVariableName){
    81.             foreach (Transform spawnPoint in pointsToSpawn)
    82.             {
    83.                 // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
    84.                 Debug.Log("I reached this point 4");
    85.                 Debug.Log(near.Count);
    86.                 GameObject instance = (GameObject)Instantiate(enemy, spawnPoint.position, spawnPoint.rotation);
    87.                 instance.transform.Rotate(Vector3.up, Random.Range(0f, 360f));
    88.                 Debug.Log("I reached this point 5");
    89.                 Debug.Log(near.Count);
    90.             }
    91.         }
    92.     }
    93. }

    The only debug message i get is the one which is in the Start method ( Debug.Log("I reached this point 1");), and i get it only when i start play mode.

    Enemies are not spawning, any idea of why? PD: I keep getting the message: "Trying to Invoke method: EnemyManager.Spawn couldn't be called."

    Thanks :=)
     
  34. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    You could swap it over to a timed event.... (I usually don't rely on repeating or timeouts.)
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4.  
    5. public class EnemyManager : MonoBehaviour
    6. {
    7.     public PlayerHealth playerHealth;       // Reference to the player's heatlh.
    8.     public Transform player;            // The position that that camera will be following.
    9.     public GameObject enemy;                // The enemy prefab to be spawned.
    10.     public float spawnTime = 3f;            // How long between each spawn.
    11.     public float spawnDistance = 10;
    12.     public float spawnscore = 100f;
    13.     public float maxspawnscore = 100f;
    14.     public Transform[] spawnPoints;         // An array of the spawn points this enemy can spawn from.
    15.     private float nextSpawn = 0;
    16.  
    17.     void Start()
    18.     {
    19.         // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
    20.         nextSpawn = Time.time + spawnTime;
    21.         Debug.Log("I reached this point 1");
    22.     }
    23.    
    24.     void Update(){
    25.         if(Time.time >= nextSpawn){
    26.             Spawn();
    27.             nextSpawn += spawnTime;
    28.         }
    29.     }
    30.  
    31.     void Spawn(int count = 1)
    32.     {
    33.         // If the player has no health left...
    34.         if (playerHealth.currentHealth <= 0f)
    35.         {
    36.             // ... exit the function.
    37.             return;
    38.         }
    39.  
    40.         if (ScoreManager.score < spawnscore)
    41.         {
    42.             return;
    43.         }
    44.  
    45.         if (ScoreManager.score >= maxspawnscore)
    46.         {
    47.             return;
    48.         }
    49.  
    50.         // collect the children that are close.
    51.         List<Transform> near = new List<Transform>();
    52.         for (int i = 0; i < transform.childCount; i++)
    53.         {
    54.             Transform spawnPoint = transform.GetChild(i);
    55.             if (Vector3.Distance(player.transform.position, spawnPoint.position) <= spawnDistance)
    56.             {
    57.                 near.Add(spawnPoint);
    58.             }
    59.             Debug.Log("I reached this point 2");
    60.             Debug.Log(near.Count);
    61.         }
    62.  
    63.  
    64.         if (near.Count > 0)
    65.         {
    66.             // create a new list here.... same as line 29, but call the variable something else.
    67.             List<Transform> pointsToSpawn = new List<Transform>();
    68.  
    69.             // loop from 0 to <count here..... same as line 30 in the code you just had. {
    70.             // make sure you include the below lines....
    71.             for (int i = 0; i < count; i++)
    72.             {
    73.                 // Find a random index between zero and one less than the number of spawn points.
    74.                 int spawnPointIndex = Random.Range(0, near.Count);
    75.  
    76.                 // add near[spawnPointIndex] to the new list // same as line 35
    77.                 pointsToSpawn.Add(near[spawnPointIndex]);
    78.  
    79.                 // remove spawnPointIndex from near // new line: near.RemoveAt(spawnPointIndex);
    80.                 near.RemoveAt(spawnPointIndex);
    81.  
    82.                 // if we dont have any more points. break... // new line: Break;
    83.                 if (near.Count == 0) break;
    84.                 Debug.Log("I reached this point 3");
    85.                 Debug.Log(near.Count);
    86.                 // end the loop... }
    87.             }
    88.  
    89.             // loop through each of your new variable.... new line: foreach(Transform spawnPoint in newVariableName){
    90.             foreach (Transform spawnPoint in pointsToSpawn)
    91.             {
    92.                 // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
    93.                 Debug.Log("I reached this point 4");
    94.                 Debug.Log(near.Count);
    95.                 GameObject instance = (GameObject)Instantiate(enemy, spawnPoint.position, spawnPoint.rotation);
    96.                 instance.transform.Rotate(Vector3.up, Random.Range(0f, 360f));
    97.                 Debug.Log("I reached this point 5");
    98.                 Debug.Log(near.Count);
    99.             }
    100.         }
    101.     }
    102. }
    103.  
     
  35. xtomyserrax

    xtomyserrax

    Joined:
    Nov 16, 2015
    Posts:
    75
    WOW! Thank you very very much! Now it does work and better than before :D. Thanks for making me this great spawning system. You are god!

    If i have any problem, (thing i dont think ill have), i will tell you!. And of course, if you have a better idea for it, tell me :D

    Again, thank you very much! Thank you and thank you!
     
  36. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
  37. xtomyserrax

    xtomyserrax

    Joined:
    Nov 16, 2015
    Posts:
    75
    I will check that! Thank you very much for your help!

    Now i will continue with the project. I have shooting to make now :D

    I would like to have you in skype. If you want my skype username is tomyserra18, if you dont want to add me dont do it.

    Thanks again! You are the best. And thanks for your patience!