Straighten to-the-point great video shows how to do everything fluidly easily took this code and reused it in my 2D ragdoll game spawning ragdoll enemys 😂🎉 would be super nice for explanation of setting up a counter to regulate the spawn. I understand how they work, just don't know c# enough yet.
Hi thanks for the vid! Say can you tell how to adjust the code to have a set number of spawns? For example 10. And every time spawn is killed the number goes back to 10 ?
My enemies are not following my player, should i attach the enemy script to player aswell?? public class Enemies : MonoBehaviour { [SerializeField] public float spawnrate = 1f; [SerializeField] public GameObject[] enemyPrefab; // Start is called before the first frame update void Start() { StartCoroutine(spawner()); } private IEnumerator spawner() { WaitForSeconds wait = new WaitForSeconds(spawnrate); while (true) { yield return wait; int Rand = Random.Range(0, enemyPrefab.Length); float positionX = Random.Range(-8,8); float positionY = Random.Range(-2, -4); GameObject enemyTospawn = enemyPrefab[Rand]; Instantiate(enemyTospawn, new Vector3(positionX, positionY, 0f), Quaternion.identity); } } }
sidenote: Im disabling the object it is boss that spawn enemies and if the player dies it is disabling every GameObject that is attached to it and reseting his coordinates back to his place.
@@ModdingByKaupenjoe Oh my... it actually worked... thanks :) I didnt know that this function even exists. If I understood it right, OnEnable() instead of doing something before the first frame updates like Start(), its doing something each time youre enabling an object again, right?
Yes, it basically gets called every time the object is enabled which includes the first time entering play mode :) (also I know it's not good etiquette but after you're done with the unity tutorials on this channel, I actually _just_ launched a new Unity Channel: www.youtube.com/@GameDevelopmentByKaupenjoe ) - definitely recommended checking it out, if unity interests you 🙏🏻
SO love it thank u.. one thing how do i offset the spawner so its middle isnt 000? I want to offset it by 100 on the x axis and dont really know how can anyone help with that?
great video ! how would i limit this as i want enemies to spawn relatively quick but i dont want there to be hundreds on my screen at a time , as im still pretty new to unity i still have no clue what im doing so any help would be appreciated :D
Instead of making it so that the "new Vector3" is random.range, make it into the position that you want it to be, for instance "Enemy, new Vector3(8.42f, -3.52f, 0f), Quaternion.identity" Hope it helps, though I am a month late
like others, i'm suffering from the issue of the original enemy being killed and this breaks the spawner, anyone have a solution? could you give any ideas Kaupenjoe?
@@ModdingByKaupenjoe im gonna be honest with you chief, while i was talking to my friend about it i went to go through the steps again that causes the issue so i could take a screenshot for them and it just, isn't a problem anymore. you might be right though, probably are, i bet i was doing that but i could have swore i tried it the other way with the same result, in any case im now onto the issue of learning how to properly get animations to play when i collide with an enemy so wish me luck, so far i've just gotten errors heh...
Great explanation, I had a question, I have a spawn area similar to this, enemies spawn correctly in the rectangular area, I also have a boss, and when the boss appears, there is a new set of enemies that appear in a circular area around the boss to make a bigger fight. It all works so far except when the boss gets near the edge of the rectangular area, when the circular spawn area of the boss overlaps with the outside of the rectangular area, the enemies can now spawn in the forbidden zone, I assume this is bc the circular spawn area is somehow taking precedence over the boundary of the rectangle, I can figure out what's next, do I need to raycast to detect where the enemy will spawn before it instantiates and if it's outside the rectangular area, don't spawn?
Thank you :) I would say, maybe you'd have to validate the position before spawning the enemy. It probably requires just a bit of tweaking of the original code. High level overview might be a while loop that is true until an enemy is spawned (this would only result in an infinite loop, if the circle is completely outside the rectangle, so be cognizant of that!). Inside the while loop, you create a random vector inside the circle, then validate if it is also inside the rectangle, if not the while loop continues and creates a new random vector until the position is in both circle and rectangle. There are probably more optimized ways of doing it as well, but that's the first thing that came to mind. Hope that helps :)
I keep getting this error each time I use SerializeFeild Assets\EnemySpawner.cs(10,10): error CS0246: The type or namespace name 'serializeFeild' could not be found (are you missing a using directive or an assembly reference?) Any tips on how to fix it????
Update - I got rid of SerializeField and the game runs but nothings spawns and this is the error that plays while the game runs ArgumentException: The Object you want to instantiate is null.
@@ModdingByKaupenjoe this is my code public class EnemySpawner : MonoBehaviour { [SerializeFeild] private float EnemyObjectSpawnerInterval = 3.5f; [SerializeFeild] private GameObject EnemyObjectSpawnerPrefab;
// Start is called before the first frame update void Start() {
} // Update is called once per frame void Update() { StartCoroutine(SpawnEnemyObject(EnemyObjectSpawnerInterval, EnemyObjectSpawnerPrefab)); } private IEnumerator SpawnEnemyObject(float interval, GameObject EnemyObject) { yield return new WaitForSeconds(interval); GameObject newEnemyObject = Instantiate(EnemyObject, new Vector3(Random.Range( -5f, 5), Random.Range(-6f, 6f),0), Quaternion. identity); StartCoroutine(SpawnEnemyObject(interval, EnemyObject)); }
} I think I may have gotten confused on where to put my EnemyObject (which is my sprite that follows the player around) in the code because the names are different to yours. but i dont think thats the problem because its just the SerializedField part which is saying its wrong ( I spelt everything the way you did)
Could you please explain how to prevent enemies to spawn inside or too near to the player? I cant solve the problem by myself (im quiet new to unity and want to learn by doing stuff)
The first thing I can think of is to make a Bounds box around the player, instead of just doing Instatiate inside the spawnEnemy method you'd make a for loop that tries until the Random Vector you're generating it not contained within the Bounds. Might be a bit complicated, if you say you're new, but that's the only (quick and easy) thing I can think of right now 😅🙏🏻
@@ModdingByKaupenjoe I did it with a do while loop. I created the 3 positions and cheked if they were too close to the player with Vector3.Distance() and then regenerated a position again and again until it is randomly far enough for the player :) thank you
Unity baffles me, i used a different tutorial so im not sure how much this relates but i created my enemy prefabs which spawned in correctly but they would never follow my character only the original prefab which was already placed in the scene, so i thought, maybe its because im using a transform instead of a GameObject and tags, but i couldnt figure that out considering i would have to rewrite some of my enemies movement. So i ended up here and realised i hadnt serialized the fields, which actually makes the prefabs follow my character, but now when i destroy the original prefab in the scene the game stops because i've destroyed the gameobject that its referring to. Now ive just updated the prefabs and its gone back to how it was before ffs
Nice and easy explained. Wouldn't you normally just make an endless loop in the coroutine instead of instantiating a new one? Something like: while (true) { yield return ... GameObject nemEnemy = ... } This way you can easily replace the true with a field variable and stop the spwaning at will. Thanks for sharing.
You can't do that because while you are in that loop, nothing else can happen in the game. Imagine you say "while 'w' is pressed, move." Sure, you would move, but until you release 'w' nothing else in the system gets computed.
@@humancreepers But, that is what the "yield return" part is for. Yield release control back to the main thread, so the coroutine doesn't freeze the game for you. while(true) would make an endless loop and a thing like while(isAlive) will keep the loop running as long as the boolean isAlive is set to true.
im also new to unity but i think reducing the range and moving the spawnpoint to that area would work or u can just replace the vector3 which gives the range, to gameObject.transform.position
Thanks for this video. Really appreciate it. Great explanation. Everything is clear.👍
Thank you! Keep up the good work!
Straighten to-the-point great video shows how to do everything fluidly easily took this code and reused it in my 2D ragdoll game spawning ragdoll enemys 😂🎉 would be super nice for explanation of setting up a counter to regulate the spawn. I understand how they work, just don't know c# enough yet.
Hi thanks for the vid! Say can you tell how to adjust the code to have a set number of spawns? For example 10. And every time spawn is killed the number goes back to 10 ?
My enemies are not following my player, should i attach the enemy script to player aswell??
public class Enemies : MonoBehaviour
{
[SerializeField] public float spawnrate = 1f;
[SerializeField] public GameObject[] enemyPrefab;
// Start is called before the first frame update
void Start()
{
StartCoroutine(spawner());
}
private IEnumerator spawner()
{
WaitForSeconds wait = new WaitForSeconds(spawnrate);
while (true)
{
yield return wait;
int Rand = Random.Range(0, enemyPrefab.Length);
float positionX = Random.Range(-8,8);
float positionY = Random.Range(-2, -4);
GameObject enemyTospawn = enemyPrefab[Rand];
Instantiate(enemyTospawn, new Vector3(positionX, positionY, 0f), Quaternion.identity);
}
}
}
where to find your previous video?
it gives me an error, it says that the name instantiate does not exist in the current context, why??
When the original enemy dies, no more enemies will spawn. Any solutions?
try dragging in the prefab rather than the original enemy in the hierarchy
I have an issue, when im disabling GameObject that spawns enemies, then enabling the same GameObject again, it wont spawn any. How could I fix it?
sidenote: Im disabling the object it is boss that spawn enemies and if the player dies it is disabling every GameObject that is attached to it and reseting his coordinates back to his place.
You might be able to put the StartCoroutine call in the OnEable method 🤔
@@ModdingByKaupenjoe Oh my... it actually worked... thanks :) I didnt know that this function even exists. If I understood it right, OnEnable() instead of doing something before the first frame updates like Start(), its doing something each time youre enabling an object again, right?
Yes, it basically gets called every time the object is enabled which includes the first time entering play mode :)
(also I know it's not good etiquette but after you're done with the unity tutorials on this channel, I actually _just_ launched a new Unity Channel: www.youtube.com/@GameDevelopmentByKaupenjoe ) - definitely recommended checking it out, if unity interests you 🙏🏻
My objects can spawn inside the walls on my scene. Do u have any ideas how to fix it?
Amazing tutorial! thank you so much! 👍
How do i change the rotation?????
when the enemies are spawned the health controller stops working
When you create two spawners it spawns in the first but not in the second spawner. Can someone fix this?
this is underrated !
Thank you so much for the kind words
@@ModdingByKaupenjoe how to add counter
w pfp
Bro, how to make a set of enemies coming in a straight line or forming sine wave or in a circle formation like space shooter games.
Did you get your answers bro
How's life btw
@@callofbooty7717 Now I am a 3d artist 😂
SO love it thank u.. one thing how do i offset the spawner so its middle isnt 000? I want to offset it by 100 on the x axis and dont really know how can anyone help with that?
THANKS A LOT, THIS IS AWSOME!
Glad you like it! 😎
you can access prefabs using an object pooler too. you can also read from resources.
great video ! how would i limit this as i want enemies to spawn relatively quick but i dont want there to be hundreds on my screen at a time , as im still pretty new to unity i still have no clue what im doing so any help would be appreciated :D
How do make my enemy’s spawn in 1 particular area
Instead of making it so that the "new Vector3" is random.range, make it into the position that you want it to be, for instance "Enemy, new Vector3(8.42f, -3.52f, 0f), Quaternion.identity"
Hope it helps, though I am a month late
like others, i'm suffering from the issue of the original enemy being killed and this breaks the spawner, anyone have a solution? could you give any ideas Kaupenjoe?
Hey There,
AH, it could be the case that you took the game objects from the scene instead of the project folder. Can you double check that? :)
@@ModdingByKaupenjoe im gonna be honest with you chief, while i was talking to my friend about it i went to go through the steps again that causes the issue so i could take a screenshot for them and it just, isn't a problem anymore. you might be right though, probably are, i bet i was doing that but i could have swore i tried it the other way with the same result, in any case im now onto the issue of learning how to properly get animations to play when i collide with an enemy so wish me luck, so far i've just gotten errors heh...
@@ModdingByKaupenjoe thank you for taking the time to respond however, it really means a lot to me! and i really enjoyed your video!
Great explanation, I had a question, I have a spawn area similar to this, enemies spawn correctly in the rectangular area, I also have a boss, and when the boss appears, there is a new set of enemies that appear in a circular area around the boss to make a bigger fight. It all works so far except when the boss gets near the edge of the rectangular area, when the circular spawn area of the boss overlaps with the outside of the rectangular area, the enemies can now spawn in the forbidden zone, I assume this is bc the circular spawn area is somehow taking precedence over the boundary of the rectangle, I can figure out what's next, do I need to raycast to detect where the enemy will spawn before it instantiates and if it's outside the rectangular area, don't spawn?
Thank you :)
I would say, maybe you'd have to validate the position before spawning the enemy. It probably requires just a bit of tweaking of the original code. High level overview might be a while loop that is true until an enemy is spawned (this would only result in an infinite loop, if the circle is completely outside the rectangle, so be cognizant of that!). Inside the while loop, you create a random vector inside the circle, then validate if it is also inside the rectangle, if not the while loop continues and creates a new random vector until the position is in both circle and rectangle.
There are probably more optimized ways of doing it as well, but that's the first thing that came to mind. Hope that helps :)
amazing script thanks for making
didnt know you also did unity tutorials!
I keep getting this error each time I use SerializeFeild Assets\EnemySpawner.cs(10,10): error CS0246: The type or namespace name 'serializeFeild' could not be found (are you missing a using directive or an assembly reference?) Any tips on how to fix it????
Update - I got rid of SerializeField and the game runs but nothings spawns and this is the error that plays while the game runs ArgumentException: The Object you want to instantiate is null.
Did you just write SerializeField wrong? because your error said: The type or namespace name *'serializeFeild'* 🤔
@@ModdingByKaupenjoe this is my code
public class EnemySpawner : MonoBehaviour
{
[SerializeFeild]
private float EnemyObjectSpawnerInterval = 3.5f;
[SerializeFeild]
private GameObject EnemyObjectSpawnerPrefab;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
StartCoroutine(SpawnEnemyObject(EnemyObjectSpawnerInterval, EnemyObjectSpawnerPrefab));
}
private IEnumerator SpawnEnemyObject(float interval, GameObject EnemyObject)
{
yield return new WaitForSeconds(interval);
GameObject newEnemyObject = Instantiate(EnemyObject, new Vector3(Random.Range( -5f, 5), Random.Range(-6f, 6f),0), Quaternion. identity);
StartCoroutine(SpawnEnemyObject(interval, EnemyObject));
}
}
I think I may have gotten confused on where to put my EnemyObject (which is my sprite that follows the player around) in the code because the names are different to yours. but i dont think thats the problem because its just the SerializedField part which is saying its wrong ( I spelt everything the way you did)
TY SIR
hey so am having this problem saying "'Quaternion' does not contain a definition for 'indentity'"
I had the same problem I just redid the part that wasn’t working
it's 'identity' not 'indentity'
@@EmperMiner1990 yea i figured 😂
when i made the spawner it ignores the time delay and just spawns something constantly
sorry i should mention that i made a different spawner lol
Could you please explain how to prevent enemies to spawn inside or too near to the player? I cant solve the problem by myself (im quiet new to unity and want to learn by doing stuff)
The first thing I can think of is to make a Bounds box around the player, instead of just doing Instatiate inside the spawnEnemy method you'd make a for loop that tries until the Random Vector you're generating it not contained within the Bounds.
Might be a bit complicated, if you say you're new, but that's the only (quick and easy) thing I can think of right now 😅🙏🏻
@@ModdingByKaupenjoe I did it with a do while loop. I created the 3 positions and cheked if they were too close to the player with Vector3.Distance() and then regenerated a position again and again until it is randomly far enough for the player :) thank you
ignore my comment bro youre the goat lol i guess the version in the tutorial i was following was outdated?
Mine says SerializeFields could not be found
Make sure its [SerializeField], and not [SerializeField(s)], their is not a s
Unity baffles me, i used a different tutorial so im not sure how much this relates but i created my enemy prefabs which spawned in correctly but they would never follow my character only the original prefab which was already placed in the scene, so i thought, maybe its because im using a transform instead of a GameObject and tags, but i couldnt figure that out considering i would have to rewrite some of my enemies movement. So i ended up here and realised i hadnt serialized the fields, which actually makes the prefabs follow my character, but now when i destroy the original prefab in the scene the game stops because i've destroyed the gameobject that its referring to. Now ive just updated the prefabs and its gone back to how it was before ffs
Try to use public variables instead of private variables?
thanks for the video
after some time the spawner destroy itself wtf
Thank you so much.
You are welcome!
Great
thanks
Nice and easy explained. Wouldn't you normally just make an endless loop in the coroutine instead of instantiating a new one? Something like:
while (true) {
yield return ...
GameObject nemEnemy = ...
}
This way you can easily replace the true with a field variable and stop the spwaning at will.
Thanks for sharing.
You can't do that because while you are in that loop, nothing else can happen in the game. Imagine you say "while 'w' is pressed, move." Sure, you would move, but until you release 'w' nothing else in the system gets computed.
@@humancreepers But, that is what the "yield return" part is for. Yield release control back to the main thread, so the coroutine doesn't freeze the game for you. while(true) would make an endless loop and a thing like while(isAlive) will keep the loop running as long as the boolean isAlive is set to true.
How do I make my enemy’s spawn in 1 particular area
im also new to unity but i think reducing the range and moving the spawnpoint to that area would work
or u can just replace the vector3 which gives the range, to gameObject.transform.position