To limit how many enemies are on the screen at once: 1) Make a new variable called enemies spawned, and one called max enemies public int eneimesSpawned = 0; public int maxEnemies; 2) Inside of void Update () put this code: if (enemiesMax
oh well yeah that's true I kinda assumed if someone wanted to add that feature that was an extra step for them to take. I just made sure that this code was proofed for it, so they don't come accross a bug they can't fix.
For those having troubles with continuous spawning, check and see if you used Enemies.Length + 1 when using random Range to decide what enemy to spawn. if you did then remove the "+ 1" to fix the problem.
Here's the sctript for those who want it. (Possible typos) using System.Collections; using UnityEngine; public class Spawner : MonoBehaviour { public GameObject [] ennemies; public Vector3 spawnValues; public float spawnWait; public float spawnMostWait; public float spawnLeastWait; public int startWait; public bool stop; int randEnemy; void Start () { StartCoroutine(waitSpawner()); } void Update () { spawnWait = Random.Range (spawnLeastWait, spawnMostWait); } IEnumerator waitSpawner() { yield return new WaitForSeconds (startWait); while (!stop) { randEnemy = Random.Range (0, 2); Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), 1, Random.Range (-spawnValues.z, spawnValues.z)); Instantiate (ennemies[randEnemy], spawnPosition + transform.TransformPoint (0, 0, 0), gameObject.transform.rotation); yield return new WaitForSeconds (spawnWait); } } }
Just what I was looking for. After searching through tons of videos, finally came across yours and it does exactly what I was wanting to do. Thanks so much for this. (from Canada, we say 'Zed')!
Those having issues with continues spawning, you have to change the value for the randEnemy randEnemy = UnityEngine.Random.Range(0, 2); over here change the number according to your needs e.g, if there is one enemy change it to 0, 1, if 2 enemies 0, 2 and if 3 enemies, 0, 3 and so on!!!! this is how I fixed my issue!!!
public GameObject[] enemies; public Vector3 spawmValues; public float spawmWait; public float spawmMostWait; public float spawmLeastWait; public int startWait; public bool stop; int randEnemy; public float speed = 10; // Start is called before the first frame update void Start() {
StartCoroutine(waitSpawmer()); } // Update is called once per frame void Update() {
Thanks a lot for sharing this. I found this tutorial very well explained and super interesting, you should try sharing the script for ur futur tutorials(on Github or anywhere).
check and see if you used Enemies.Length + 1 when using random Range to decide what enemy to spawn. if you did then remove the "+ 1" to fix the problem.
hi, thank you for the tutorial, it's very helpful. I was wondering if you could make a tutorial on how to spawn random objects at random times and position with the same set of objects (recycling them or reusing the object once they are of the screen or when the object is needed.) sorry if this is confusing. Thank you :)
Thanks for the tutorial. You should put code where you assign random value to spawnWait from Update method to the instruction before yield return new WaitForSecords as you only need one generation. so the longer spawnToWait is gonna be the more generations will go to waste :)
If you want visualize where the objects are going to spawn, simply add this method to the spawner script. private void OnDrawGizmosSelected() { Gizmos.color = Color.red; Gizmos.DrawCube(transform.position, spawnValues); }
Hey Man i love the tutorial! i was wondering if you would be able to tell me how i would go about making it so there will only ever be say 3 spawned objects in the scene at one time and once one get destroyed another one will spawn? keep up the good work! :)
Hi, i need some help! i want all my enemies to attack a base, which the player will protect (in 3d). They will deal damage once the collide with the base; so here is my problem. i want the enemies to move towards the base *but* i don't want them to target a single vector 3 point, since the base is large, it looks very weird when they meet up at one specific place. I tried to change the target position to a random position within a certain range, but with my code it only makes them changing path every time the code updates, since that happens very fast it just looks ridiculous. here is the code that i am using (which is basicly one that found at answers.unity3d, with some modification): using System.Collections; using System.Collections.Generic; using UnityEngine; public class CubeB : MonoBehaviour { public float moveSpeed = 5; *//move speed* public float rotationSpeed = 5; *//speed of turning* private Rigidbody rb; //private Vector3 mytransform = target; Vector3 targetO = new Vector3(0,0,6); void Start() { rb = GetComponent(); } void Update() { *//decide the target the cubes shall moves towards* targetO += new Vector3(0, 0, 0.01f); Vector3 target = new Vector3( Random.Range(targetO.x - 25, targetO.x + 25), Random.Range(targetO.y - 25, targetO.y + 25), Random.Range(targetO.z - 25, targetO.z + 25)); *//rotate to look at the base* transform.rotation = Quaternion.Slerp( transform.rotation, Quaternion.LookRotation(target - transform.position), rotationSpeed * Time.deltaTime); *//move towards the base* transform.position += transform.forward * Time.deltaTime * moveSpeed; } }
thanks for making this tutorial, i have some query, suppose i want to make stop spawning enemy after 10 , or some user define numbers of enemy , what should i do ...plz help am bad in scripting , i know i have to declare public variable to asign number of enemy to be spawn , what should i do , should i make a new method for example "void noOfEnemy" i tried but did't get work
around timecode 8 minutes, you have random.range(0,2) - wouldn't you be better off with random.range(0,enemies.length+1) so that you don't have a "magic number"in your code?
sir.using your code how can i limit the spawn of the enmies...like my max enmies will b 8 only... and evrytime 1 of the enmy will be destroyed it will spawn again as long as they reach the max spawn.. can you teach me how??
Extremely late, but here is what I would do. Make an int called something like enemyCount and set it to 0. Every time an enemy is spawned through your coroutine, make enemyCount += 1. From there have an if statement in your Update method that says something like if(enemyCount >= enemyLimit){Stop Coroutine("blah blah")}
1) Make a new variable called enemies spawned, and one called max enemies public int eneimesSpawned = 0; public int maxEnemies; 2) Inside of void Update () put this code: if (enemiesMax
It would be more efficient to put the spawnWait = Random.Range(spawnLeastWait, spawnMostWait); just before the yield return new WaitForSeconds(spawnWait); that way you don't generate a new number every frame. Not really a big deal, it just annoyed me seeing it constantly change in unity.
How would you implement odds, for example, the blue enemy has a chance of 60% to spawn, red 40%? I was thinking about adding blue 6 times/red 4 times, however that would be quite messy, especially when you try adding more objects and more odds. What would you suggest? :)) Also thank you very much for the tutorial, helped a lot so far ^-^
make sure the following lines have the correct amount of Enemies: while (!stop) { randEnemy = Random.Range(0, 98); ensuring you have attached the gameobjects
Thank you soo much! This helped me on a game I'm working where a random civillian must spawn then enter a bus. My question though is how do I make it stop repeating the same civillian/enemy? Like when it already spawns a specific AI it wont generate again?
make a c#called MovethatThing add this from start to finish using System.Collections; using System.Collections.Generic; using UnityEngine; public class MoveThatThing : MonoBehaviour { public float speed = 0f;
void Start() { speed = 0.003f;
} void Update() { transform.Translate(speed, 0, 0); } } save it and then attach to a prefab or a cube if u like it will move but u need to add the speed using the 'Inspoctor' on the right ie try putting in 5 or 7 etc cheers
Hi, my enemy is coming but it is not moving. Could you please tell me how to move the enemy in on direction? Or could you share the enemy object moving script?
try this but first make a new C# script must be called MoveThatThing using System.Collections; using System.Collections.Generic; using UnityEngine; public class MoveThatThing : MonoBehaviour { public float speed = 0f;
I cant see anything wrong with my code but when I look in unity I'm supposed to drag and drop my enemy prefab in but it says size and I can put a number in help? can you see anything wrong with my code using System.Collections; using System.Collections.Generic; using UnityEngine; public class Spawner : MonoBehaviour { public GameObject[] Enemies; public Vector3 spawnValues; public float spawnWait; public float spawnMostWait; public float spawnLeastWait; public int startWait; public bool stop; int randEnemy; // Use this for initialization void Start () { StartCoroutine(WaitSpawner()); } // Update is called once per frame void Update () { spawnWait = Random.Range (spawnLeastWait, spawnMostWait); } IEnumerator WaitSpawner() { yield return new WaitForSeconds (startWait); while (!stop) { randEnemy = Random.Range (0, 2); Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), 1, Random.Range (-spawnValues.z, spawnValues.z)); Instantiate (Enemies[randEnemy], spawnPosition + transform.TransformPoint (0, 0, 0), gameObject.transform.rotation); yield return new WaitForSeconds (spawnWait); } } }
You could add more to the amount of variety enemies could spawn, for example, instead of just 2 different cubes, there could be 10 different cubes, and you could make each of them different speeds. Of course this would be better with scripts, but if you can't script and absolutely can't find a solution then this will work
Make a new script and write this in update: void Update() { transform.position += Vector3.back *10* Time.deltaTime; } then put it on the game object that will move.. you can change 10, if you want it to be faster.. and you can change :back to forward, right or left.. Sorry if it was late
Here is my code and it works so you can compare it. using System.Collections; using System.Collections.Generic; using UnityEngine; public class Spawner : MonoBehaviour { public GameObject[] enemies; public Vector3 spawnValues; public float spawnWait; public float spawnMostWait; public float spawnLeastWait; public int startWait; public bool stop; int randEnemy; void Start() { StartCoroutine(waitSpawner()); } void Update() { spawnWait = Random.Range (spawnLeastWait, spawnMostWait); } IEnumerator waitSpawner() { yield return new WaitForSeconds(startWait); while (!stop) { randEnemy = Random.Range (0, 2); Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), 1, Random.Range (-spawnValues.z, spawnValues.z)); Instantiate (enemies[randEnemy], spawnPosition + transform.TransformPoint (0, 0, 0), gameObject.transform.rotation); yield return new WaitForSeconds (spawnWait); } } }
yes, but than use arrays to decide in advance where you want your "tracks", then you could use his code to make when enemies appear randomly, such as he does here.
[SOLVED ]It says "Missing Game Object" whenever I start the game. Both the blue and Red cubes are prefabs and defined as variables. Does anybody else have this problem?
spawnpoint in the middle is the gameobject . also his cubes already had a c# script attached to them that moves them independently of his randomspawn script.
Julian Martinez Sign up to Google Play Console, it costs £25 one-off and you can publish as many games as you want. You’d need to export them from Unity as an .apk file. Apple is more expensive and complicated.
here is what I would do. Make an int called something like enemyCount and set it to 0. Every time an enemy is spawned through your coroutine, make enemyCount += 1. From there have an if statement in your Update method that says something like if(enemyCount >= enemyLimit){Stop Coroutine("blah blah")}
ideal improvement would be to color correct your objects shadows. As they are red and blue, theres optimal shadowcolors that you can add based on bounce and color.
Even eight years later, this still holds up
To limit how many enemies are on the screen at once:
1) Make a new variable called enemies spawned, and one called max enemies
public int eneimesSpawned = 0;
public int maxEnemies;
2) Inside of void Update () put this code:
if (enemiesMax
your method works .. but when maxenemy has been reached it stays on stop although there are no more
That's why there is the else. The else makes it so that when there is room for more it lets them spawn again.
enemy spawn will not go down even though the enemies are gone ..
oh well yeah that's true I kinda assumed if someone wanted to add that feature that was an extra step for them to take. I just made sure that this code was proofed for it, so they don't come accross a bug they can't fix.
do you have a tip how can I fix it ??
For those having troubles with continuous spawning, check and see if you used Enemies.Length + 1 when using random Range to decide what enemy to spawn. if you did then remove the "+ 1" to fix the problem.
Here's the sctript for those who want it. (Possible typos)
using System.Collections;
using UnityEngine;
public class Spawner : MonoBehaviour {
public GameObject [] ennemies;
public Vector3 spawnValues;
public float spawnWait;
public float spawnMostWait;
public float spawnLeastWait;
public int startWait;
public bool stop;
int randEnemy;
void Start ()
{
StartCoroutine(waitSpawner());
}
void Update ()
{
spawnWait = Random.Range (spawnLeastWait, spawnMostWait);
}
IEnumerator waitSpawner()
{
yield return new WaitForSeconds (startWait);
while (!stop)
{
randEnemy = Random.Range (0, 2);
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), 1, Random.Range (-spawnValues.z, spawnValues.z));
Instantiate (ennemies[randEnemy], spawnPosition + transform.TransformPoint (0, 0, 0), gameObject.transform.rotation);
yield return new WaitForSeconds (spawnWait);
}
}
}
thank you
That is gud
Thank you
thanks
Just what I was looking for. After searching through tons of videos, finally came across yours and it does exactly what I was wanting to do. Thanks so much for this. (from Canada, we say 'Zed')!
Those having issues with continues spawning, you have to change the value for the randEnemy
randEnemy = UnityEngine.Random.Range(0, 2);
over here change the number according to your needs e.g, if there is one enemy change it to
0, 1, if 2 enemies 0, 2 and if 3 enemies, 0, 3 and so on!!!!
this is how I fixed my issue!!!
thanks man.
I followed the tutorial, but I only get a few "enemies" usually around four and then the spawning stops. What should I do?
Damm, I am a biggener but I understood unexpectedly well, u should have more sub's than this, u deserve it mate!!!!
I did also sub to you just so you know!!!
This really helped me a lot, even as a beginner i could follow the explanation really well.
Great job !!!
Awesome tutorial, like MBoost said, explains exactly what i couldn't figure out.
this helped a lot tysm
public GameObject[] enemies;
public Vector3 spawmValues;
public float spawmWait;
public float spawmMostWait;
public float spawmLeastWait;
public int startWait;
public bool stop;
int randEnemy;
public float speed = 10;
// Start is called before the first frame update
void Start()
{
StartCoroutine(waitSpawmer());
}
// Update is called once per frame
void Update()
{
spawmWait = Random.Range(spawmLeastWait, spawmMostWait);
// transform.Translate(0, 0, -speed * Time.deltaTime);
}
IEnumerator waitSpawmer()
{
yield return new WaitForSeconds(startWait);
while (!stop)
{
randEnemy = Random.Range(0, 2);
Vector3 spawnPosition = new Vector3(Random.Range(-spawmValues.x, spawmValues.x), 1, Random.Range(-spawmValues.z, spawmValues.z));
Instantiate(enemies[randEnemy], spawnPosition + transform.TransformPoint(0, 0, 0), gameObject.transform.rotation);
yield return new WaitForSeconds(spawmWait);
}
}
Thanks for putting this together. This i exactly what I was looking for.
So awesome. Great explanations as to what everything is and why to use it.
thanks alots, I wasn't exactly looking for how to randomize spawn stuff but the randomize system it self thanks again!
Thanks a lot for sharing this. I found this tutorial very well explained and super interesting, you should try sharing the script for ur futur tutorials(on Github or anywhere).
Thank you so much man! you helped shaping some of the core mechanics of my hyper casual game!
Appreciate this tutorial very much thank you. A good start to my project!
Just what i was looking for.. Thanks!! :)
Mee too
I am using your script and method in a game but after something it stops spawning, after sometime it spawn nothing!!! help me ...
same for me!!!
check and see if you used Enemies.Length + 1 when using random Range to decide what enemy to spawn. if you did then remove the "+ 1" to fix the problem.
help me unity says error CS1503: Argument 3: cannot convert from '(float, float)' to 'float'
Why not to randomize the spawnWait directly on the while loop inside the IEnumerator?
A great tutorial that explains very clearly, thank you very much.
how to stop the loop after reaching some number
hi, thank you for the tutorial, it's very helpful. I was wondering if you could make a tutorial on how to spawn random objects at random times and position with the same set of objects (recycling them or reusing the object once they are of the screen or when the object is needed.) sorry if this is confusing.
Thank you :)
How do you make spawn wait stick to only one random pick???
This only works if you want the spawn position to be around 0,0,0, How can you set it to the position of the game object that has the script applied?
Awesome! Thank you!
How do you prevent them from overlaping from each other?
Im getting a "IndexOutOfRangeException: Array index is out of range.
SpawnEnemies+c__Iterator0.MoveNext ()" what could this mean?
Mandy 03 post your code somewhere send me a link and I'll help you out.
Same Error, still dont know how to fix
This is awesome and I am glad I am subscribed!
Thanks for the tutorial. You should put code where you assign random value to spawnWait from Update method to the instruction before yield return new WaitForSecords as you only need one generation. so the longer spawnToWait is gonna be the more generations will go to waste :)
If you want visualize where the objects are going to spawn, simply add this method to the spawner script.
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawCube(transform.position, spawnValues);
}
IndexOutOfRangeException: Index was outside the bounds of the array.
RotSpawner+d__10.MoveNext () ://////////////
How do U have enemy in the script yet the cubes are named blue cube & red cube?
I had to adapt it, but this was very useful especially for a beginner
Hey Man i love the tutorial! i was wondering if you would be able to tell me how i would go about making it so there will only ever be say 3 spawned objects in the scene at one time and once one get destroyed another one will spawn?
keep up the good work! :)
Hey im using this script but my problem is that no object acctually spawns in
could someone help me out
Hi, i need some help!
i want all my enemies to attack a base, which the player will protect (in 3d). They will deal damage once the collide with the base; so here is my problem.
i want the enemies to move towards the base *but* i don't want them to target a single vector 3 point, since the base is large, it looks very weird when they meet up at one specific place. I tried to change the target position to a random position within a certain range, but with my code it only makes them changing path every time the code updates, since that happens very fast it just looks ridiculous.
here is the code that i am using (which is basicly one that found at answers.unity3d, with some modification):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CubeB : MonoBehaviour {
public float moveSpeed = 5; *//move speed*
public float rotationSpeed = 5; *//speed of turning*
private Rigidbody rb;
//private Vector3 mytransform = target;
Vector3 targetO = new Vector3(0,0,6);
void Start()
{
rb = GetComponent();
}
void Update()
{
*//decide the target the cubes shall moves towards*
targetO += new Vector3(0, 0, 0.01f);
Vector3 target = new Vector3(
Random.Range(targetO.x - 25, targetO.x + 25),
Random.Range(targetO.y - 25, targetO.y + 25),
Random.Range(targetO.z - 25, targetO.z + 25));
*//rotate to look at the base*
transform.rotation = Quaternion.Slerp(
transform.rotation,
Quaternion.LookRotation(target - transform.position),
rotationSpeed * Time.deltaTime);
*//move towards the base*
transform.position += transform.forward * Time.deltaTime * moveSpeed;
}
}
How do I make these enemies move
Thank you, looking to activate the "bool stop" inside a death script. any suggestions on how to stop the swapner please advice?
Here is some feedback: when coding, try to add zoom in & out effect, so everyone can see the code clearly
He did - at leasst in the first part, right?
I like seeing it all at once :) but to each his own
Hi ... i write same code as you write but no object is instantiating.. why?
thanks for making this tutorial, i have some query, suppose i want to make stop spawning enemy after 10 , or some user define numbers of enemy , what should i do ...plz help am bad in scripting , i know i have to declare public variable to asign number of enemy to be spawn , what should i do , should i make a new method for example "void noOfEnemy" i tried but did't get work
Thank you ! This tutorialis really useful ! I can`t belive that i can do it !
Thanks, this helped even in 2020.
around timecode 8 minutes, you have random.range(0,2) - wouldn't you be better off with random.range(0,enemies.length+1) so that you don't have a "magic number"in your code?
If I have generated 10 clones, how does one refer to the individual clones? I would appreciate the help! :)
hey hey i find.. open source code and change 2 to (how much generated clones) and working u need change look the photo hizliresim.com/2e2T9D
if working pls say :)
Thank you.
@@macleodgordonI helped someone for the first time with coding im really happy :)
Thank you for this, it worked for what I'm making! Thumbs up...
This is helpful video. I'll need your code a lot!
Thanks for the vid, this helped me out a lot :)
very useful, thanks. however this method sometimes can cause overlapping, are there any option to avoid it?
wow that was really helpful thank u! :)
Thank you so much for this vdo i need very badly thank you again.
sir.using your code how can i limit the spawn of the enmies...like my max enmies will b 8 only... and evrytime 1 of the enmy will be destroyed it will spawn again as long as they reach the max spawn.. can you teach me how??
Extremely late, but here is what I would do. Make an int called something like enemyCount and set it to 0. Every time an enemy is spawned through your coroutine, make enemyCount += 1. From there have an if statement in your Update method that says something like if(enemyCount >= enemyLimit){Stop Coroutine("blah blah")}
1) Make a new variable called enemies spawned, and one called max enemies
public int eneimesSpawned = 0;
public int maxEnemies;
2) Inside of void Update () put this code:
if (enemiesMax
It would be more efficient to put the
spawnWait = Random.Range(spawnLeastWait, spawnMostWait);
just before the
yield return new WaitForSeconds(spawnWait);
that way you don't generate a new number every frame. Not really a big deal, it just annoyed me seeing it constantly change in unity.
How would you implement odds, for example, the blue enemy has a chance of 60% to spawn, red 40%? I was thinking about adding blue 6 times/red 4 times, however that would be quite messy, especially when you try adding more objects and more odds. What would you suggest? :))
Also thank you very much for the tutorial, helped a lot so far ^-^
TimTime generate a random numer from 1 to 10. If < 5 spawn blue, if >= 5 spawn red :)
how you destroy the objects that are created, so the cpu dont exploit?
Add a script to the game object youre making and ad a method that does this after certain amount of time:
Destroy(this.gameObject);
put this in to update-- Destroy(gameObject, 3); the number is how many seconds, so in this case the 3 means you wait 3 seconds then it is destroyed.
works perfectly thanks !
Thanks Dude Helped Me A Lot
im getting array index is out of range? please help?
Me too! i need help
I'm getting the same issue.
you want to reference an element(enemy) that is not there
make sure the following lines have the correct amount of Enemies:
while (!stop)
{
randEnemy = Random.Range(0, 98);
ensuring you have attached the gameobjects
Thank you! great tutorial!
what is the difference between Startwait and SpawnWait? thanks
yes
start is star to first spawn and start spawn all objects(the secuence), spawnwait is the cooldown to another object spawn like wait next spawn.
Thank you soo much! This helped me on a game I'm working where a random civillian must spawn then enter a bus. My question though is how do I make it stop repeating the same civillian/enemy? Like when it already spawns a specific AI it wont generate again?
keep a counter each time around the while loop. Once the counter gets to a value you want, use a 'break' stmt to exit the while loop
How do I add stuff like scripts so the enemies move or rigidbodies or physics?
DasMaffin add the scripts to a enemy prefab
make a c#called MovethatThing
add this from start to finish
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveThatThing : MonoBehaviour {
public float speed = 0f;
void Start()
{
speed = 0.003f;
}
void Update()
{
transform.Translate(speed, 0, 0);
}
}
save it and then attach to a prefab or a cube if u like
it will move but u need to add the speed using the 'Inspoctor' on the right ie try putting in 5 or 7 etc
cheers
That's nice there was many things that I need to know to make my own game
Hi, my enemy is coming but it is not moving. Could you please tell me how to move the enemy in on direction? Or could you share the enemy object moving script?
try this but first make a new C# script must be called MoveThatThing
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveThatThing : MonoBehaviour {
public float speed = 0f;
void Start()
{
speed = 0.003f;
}
void Update()
{
transform.Translate(speed, 0, 0);
}
}
The spawner works okay, but now none of the cloned objects' nav meshes work
i think they dont need navmesh. his cubes are moved by another C# script attached to each of the prefabs
How to Destroy Prefabs..?
the random dont work to me always do the same value >.
I cant see anything wrong with my code but when I look in unity I'm supposed to drag and drop my enemy prefab in but it says size and I can put a number in
help? can you see anything wrong with my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour {
public GameObject[] Enemies;
public Vector3 spawnValues;
public float spawnWait;
public float spawnMostWait;
public float spawnLeastWait;
public int startWait;
public bool stop;
int randEnemy;
// Use this for initialization
void Start ()
{
StartCoroutine(WaitSpawner());
}
// Update is called once per frame
void Update ()
{
spawnWait = Random.Range (spawnLeastWait, spawnMostWait);
}
IEnumerator WaitSpawner()
{
yield return new WaitForSeconds (startWait);
while (!stop)
{
randEnemy = Random.Range (0, 2);
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), 1, Random.Range (-spawnValues.z, spawnValues.z));
Instantiate (Enemies[randEnemy], spawnPosition + transform.TransformPoint (0, 0, 0), gameObject.transform.rotation);
yield return new WaitForSeconds (spawnWait);
}
}
}
how about random speed on each cube`?
You could add more to the amount of variety enemies could spawn, for example, instead of just 2 different cubes, there could be 10 different cubes, and you could make each of them different speeds. Of course this would be better with scripts, but if you can't script and absolutely can't find a solution then this will work
Can someone help me? My object is spawning but not moving... Pretty much did everything...
Because he did a script for it, but didn't show it in the video
Make a new script and write this in update:
void Update()
{
transform.position += Vector3.back *10* Time.deltaTime;
}
then put it on the game object that will move..
you can change 10, if you want it to be faster.. and you can change :back to forward, right or left.. Sorry if it was late
it gives me errors at (-spawnValues.z, spawnValues.z) how to solve? i am using unity 2019.1.8
Here is my code and it works so you can compare it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public GameObject[] enemies;
public Vector3 spawnValues;
public float spawnWait;
public float spawnMostWait;
public float spawnLeastWait;
public int startWait;
public bool stop;
int randEnemy;
void Start()
{
StartCoroutine(waitSpawner());
}
void Update()
{
spawnWait = Random.Range (spawnLeastWait, spawnMostWait);
}
IEnumerator waitSpawner()
{
yield return new WaitForSeconds(startWait);
while (!stop)
{
randEnemy = Random.Range (0, 2);
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), 1, Random.Range (-spawnValues.z, spawnValues.z));
Instantiate (enemies[randEnemy], spawnPosition + transform.TransformPoint (0, 0, 0), gameObject.transform.rotation);
yield return new WaitForSeconds (spawnWait);
}
}
}
you could do this but with pre existed tracks? like plant vs zombies?
yes, but than use arrays to decide in advance where you want your "tracks", then you could use his code to make when enemies appear randomly, such as he does here.
[SOLVED ]It says "Missing Game Object" whenever I start the game. Both the blue and Red cubes are prefabs and defined as variables. Does anybody else have this problem?
spawnpoint in the middle is the gameobject . also his cubes already had a c# script attached to them that moves them independently of his randomspawn script.
change the name of the folder call it GameObject
@@kagigreenscreen Thanks, this worked. Problem solved.
Once I have made my game how do I export it to the play and Apple Store?
If you can't google that yourself, you probably should not make games ;)
Julian Martinez Sign up to Google Play Console, it costs £25 one-off and you can publish as many games as you want. You’d need to export them from Unity as an .apk file. Apple is more expensive and complicated.
the random works... but the spawned object doesn't move..... what did i miss?
Moving the object is a result of separate code.
you did good for your tutorial man
any idea's on how to not make the enemies touch eachother
Box collider, if it doesn't work, add a rigidbody
could I please have the code
How can I pause and resume the spawner?
if you figured it out or if any1 else know please let me know
Omfg, ur awesome!! Thank You!!
IEnumerator does not work for me help!
"StartCoroutine(TheCoroutine());"
can tis work for 2d
Very useful thank you :D
Thank you Good Tutorial
Thanks for the great tutorial man!
nice tutorial, but how can i limit the spawn?
here is what I would do. Make an int called something like enemyCount and set it to 0. Every time an enemy is spawned through your coroutine, make enemyCount += 1. From there have an if statement in your Update method that says something like if(enemyCount >= enemyLimit){Stop Coroutine("blah blah")}
the startCoroutine(waitSpawn()); doesn't eork
work*
If you named the functions the same way they are named in the tutorial, then you've no waitSpawn() function, but a waitSpawner() one
Code is not working.
Your script just freezes my unity please help
Zerail post your code and send me a link and ill help you sort this out.
Thnx for the vid gg
THANKS BRO !!
ideal improvement would be to color correct your objects shadows. As they are red and blue, theres optimal shadowcolors that you can add based on bounce and color.
a lot of thanks!!
Wow thank you this helped me a bunch!
Pls Past Code
"past"
Good stuff.
I liked the part when he said "Um"