Hi there, I am making a tower defense game for learning purposes only. At the moment I have made the map and the sprites for the game and slowly i am getting into the scripting part. In my lvl i have two paths where enemies will be spawned. At the moment I am testing to see if the spawn works with my enemies. In the code i have set successfully to see my enemy to spawn from the first spawn point.I have stuck now of how I can activate my second spawn point as well so I can carry on. This is the script from my game manager script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour {
public static GameManager instance = null;
public GameObject spawnPoint1;
public GameObject spawnPoint2;
public GameObject[] enemies;
public int maxEnemiesOnScreen;
public int totalEnemies;
public int enemiesPerSpawn;
private int enemiesOnScreen = 0;
void Awake()
{
if (instance == null)
instance = this;
else if (instance != this)
Destroy(gameObject);
DontDestroyOnLoad(gameObject);
}
// Use this for initialization
void Start () {
spawnEnemy();
}
void spawnEnemy()
{
if (enemiesPerSpawn > 0 && enemiesOnScreen < totalEnemies)
{
for(int i = 0; i < enemiesPerSpawn; i++)
{
if(enemiesOnScreen < maxEnemiesOnScreen)
{
GameObject newEnemy = Instantiate(enemies[0]) as GameObject;
newEnemy.transform.position = spawnPoint1.transform.position;
enemiesOnScreen += 1;
}
}
}
}
}
In unity i have set correctly the spawn points.So definitely it has to do with the script.Any help will be appreciated.
You could do this using an IEnumerator. The documentation is pretty clear on how that works, I am sure you will be able to figure it out. So you can have a IEnumerator for Spawning pointA and one for spawning pointB. It will also allow you to spawn the enemies one-by-one. When the one set of enemies is done, just start the second coroutine. Shout if you need more guidance.
ok,sorry for being a little bit noob,but scripting is not my strong skill, thats why i am making this project. Do I put the IEnumerator inside the void spawnEnemy? and if yes,how it should be? At the moment i would like to make the enemies to respawn at the same time from both spawnpoints.
(so far i am just testing the spawns,i havent created the waves of the enemies,the main idea is to have many waves where enemies will respawn from 2 sides. As the waves will go up,different enemies will start to appear).
What i want to make to work is to see my 2 enemies’ sprites to appear in both spawnpoints. Afterwards i will start looking for the timing. I do babysteps so i can understand what i am doing instead of copying/pasting codes XD
still doesnt work,is so annoying,it works fine with the first spawnpoint but cant find a way to make both working. i was hoping that since the spawn1 is a prefab,it will work for both points.but somehow i have to declare the second spawnpoint as well
The best way to support multiple spawn points will be to have spawning as its own script & not in the game manager.
Ill try to avoid writing the scripts out for you, so that you can try to figure it out yourself (personally find its better to stumble and fail than read someone elses code).
First of all move the spawning code to a separate script that handles spawning an amount of enemies over a certain amount of time from a specific location. You’ve got this in the game manager, but if you wanted more paths and better control other each you are going to end up with confusing code.
Once the spawning code is in its script you can put this script onto diffferent spawn points to spawn enemies from that point.
At this point you should have enemies getting spawned from multiple points, for extra control you can do this bit aswell.
Now if you make it so that a spawn point needs to be told to spawn by creating a bool that needs to be true to spawn and having a public start spawning function that sets it to true. This way you can control when the spawn points activate e.g. after one finishes you may want another to start.
The logic for this can be put into your game manager. So its in charge of turning spawners on at the right times. You can have a List of spawning scripts held in the game manager.
I may have gone abit overboard, but if theres any questions or you get stuck just ask
Just to add to LC, having the spawn point as n prefab is also overkill. You can just use n Vector2/3 variable. That way you can store the spawn points in an array. But I agree, having a dedicated spawn script is definitely the way to go.
i played with the IEnumerator inside the GameManager,i made it to work just for the 1 spawnpoint following my path(I made 1 wave XD). I need to see an example of how to build the spawn script so i can experiment with it.As for the spawn prefabs,i thought that it will work better since i put the spawn code in game manager.
so I finally made a spawn script,and i put all the code there and attached it to each spawn separately(no prefabs). here is the code.If someones has a better way to suggest me to do it please let me know.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawn : MonoBehaviour {
public GameObject spawnPoint1;
public GameObject[] enemies;
public int maxEnemiesOnSreen;
public int totalEnemies;
public int enemiesPerSpawn;
private int enemiesOnScreen = 0;
const float spawnDelay = 0.5f;
// Use this for initialization
void Start () {
StartCoroutine(spawn());
}
IEnumerator spawn()
{
if (enemiesPerSpawn > 0 && enemiesOnScreen < totalEnemies)
{
for (int i = 0; i < enemiesPerSpawn; i++ )
{
if (enemiesOnScreen < maxEnemiesOnSreen)
{
GameObject newEnemy = Instantiate(enemies[0]) as GameObject;
newEnemy.transform.position = spawnPoint1.transform.position;
enemiesOnScreen += 1;
}
}
yield return new WaitForSeconds(spawnDelay);
StartCoroutine(spawn());
}
}
public void removeEnemyFromScreen()
{
if (enemiesOnScreen > 0)
enemiesOnScreen -= 1;
}
// Update is called once per frame
void Update () {
}
}
Unfortunately i had to double my enemies(form 3 to 6) because in each enemy i have assigned a c# code where i am telling them the path to follow.
Question 1:I am in test mode,for the moment i wanted to test if the spawnpoints were working and if the animation of the enemies are not haveing any issues.Later on it will be public to manage delaytime and waves.
Question 2: I have 3 basic enemies. so i make the array because later on the system is going to choose random enemies to pop up. I thought i should have it ready so i can think of the code and everything else.If you thinkis wrong,please let me know. As i have mentioned above,i am not expert in coding,so I am trying to think as a game designer XD
Question 3: In the enemy script i have a void where i set the waypoints(the order of my checkpoints)so the enemy will follow instead of running crazy. Since the enemy script is attached in the enemy,i can’t set both ways on the same enemy(see above the map layout).So i thought to double the enemies prefabs and attached the enemy script,so each enemy will have their specific path.
I know that there must be other way to avoid these small problems. But since it works(so far until i change something XD)i let the code as it is.
Thats all fine I was just wondering. For the prefab issue if you wanted to avoid needing new prefabs for each path you could let the spawner hold the path and pass it along to anything it spawns.