First Name
Last Name
Confirm Password
Please validate your account by clicking the link in your email
Resend Validation Email

Endless Runner Level Generator in Unity


Let's create a Level Generator to make levels for a Infinite Endless Runner in Unity 2D.

Download Project Files
Post Comment


Code Monkey
Check the follow up video where we complete our Endless Runner Mini-Game with increasing difficulty and a Highscore Table
sagar rawal
can anyone please tell me how to make and image as an platform for the player to run im very beginner i only want make an 2d endless runner or a runner game with levels but there are good tutorial out there
Tejas Ramanuj
GameDev Specialist
How can I delete the prefabs after it left the screen?
emanuel moberg
I have this error message please help;
Assets\LevelGenerator.cs(11,30): error CS0246: The type or namespace name 'Player' could not be found (are you missing a using directive or an assembly reference?)
Code Monkey
You dont have any class named "Player" in your project
How do I make this to only go upwards? for my doodle jump game.
Baroian Andrei
Thanks you so much, it gave my game a purpose
Joseph Niccoli
im very close to being done with my game or so i thought when i went for a full test i noticed that my endless runner stops between 20 -42 levelparts and always on my 4thlevelpart but it uses the 4thlevelpart without issue inbetween but always ends on it and stops auto generating any idea why?
Code Monkey
Pause and check the Z position, it should be 0, it not then you're increasingly spawning platformers on a higher Z which eventually spawns behind the camera
Joseph Niccoli
@Code Monkey i noticed it was doing that but shouldnt they still show up in the hierarchy which its not? it just stops creating them
Joseph Niccoli
@Code Monkey i checked it its pushing the z axis to 99.something before it stops spawning but would that stop it from creating new platforms also how do i fix this? thanks for the help im in love with your videos
Mr Poli
Is there a way to do it without finds?
Esteban Obilinovic
can this works with tilemaps?
How to add object pooling to this ??
too many adds
Максим Холодно-теплый
Thank you very much! But sometimes it is just stop spawning
Code Monkey
Did it really stop spawning or is it spawning behind the camera?
Максим Холодно-теплый
Code Monkey it really stops, I checked, prefabs just stop spawning if I stay on one place more than 30 seconds
someone please help, my platforms are infinitely spawning but, they're spawning on top of each other
Code Monkey
Check where you placed your EndPoint
@Code Monkey thanks! I fixed it
Chaos Loved
[For my opinion] your explaination too fast for newbie like me, and not creating new, but continue that you already do, so newbie like me can't understand it :( just just my opinion.
I'm going to make a millionaire game thanks to this tutorial!! :D
Joseph Niccoli
its only randomly generating 5 then stops where am i going wrong?
Code Monkey
Maybe you're increasing their Z position and they start spawning behind the camera. Make sure the EndPosition is at z = 0
Joseph Niccoli
@Code Monkey it had something to do with the endpoint on my second prefab
This is amazing but I just wish I knew how to make a serialized field for the private player reference. It isn't as easy as just copying what you have here. Do you or any passer bys of this comment have a link to that tutorial? Thank you!
Code Monkey
There's nothing else to it, just add the SerializedField attribute and it will show up as a field in the editor
@Code Monkey thanks for getting back to me but actually the solution for me was to add this code to my player script and level part generator: Credits to Omega Gamer 101 in the comments.
public class Player : MonoBehaviour
public Vector3 GetPosition()
return transform.position;
Disscordiab Lorenzo
I am at 14:43 and they do randomly generate forever.

However they are spawning on top of eachother, it's as if they are all spawning at the same time and not selecting only one
Very simple and fluent guide, thank you! Also, if anyone want to optimize application, you can simply add to LevelPart prefabs simple self destroy script:
private float secondsToDestroy = 20f;
void Start()

IEnumerator DestroySelf()
yield return new WaitForSeconds(secondsToDestroy);
Have a nice day :)
Great idea! What if i wanted to destroy the level parts with the click of a button?
Watch a video "how to handle button on click event on Unity"
If you want to destroy all level parts on scene, I would do it like this:
Put a tag on each of levelparts, and made a ClickDestroy script, which firstly saves references to all Level Part gameObjects:
var parts = Gameobject.GetComponentsByTag("Levelpart");
And than using foreach loop - delete all objects
foreach(var obj in parts) Destroy(obj);
And than you should put this in separate function in script, and link it to button on click event.
Maybe I am wrong in some places, because I dont have opened Unity in front of me, and I wrote functions from head.
Good Luck!
@mehoff i did it like this:
public void DestroyLevelParts(){
levelParts = GameObject.FindGameObjectsWithTag("levelPart");

for(var i = 0 ; i < levelParts.Length ; i ++)

but when restarting the game you have to place the lastEndPosition back to the fiorst level part like this:
lastEndPosition = startPosition.transform.position;
Great video and definitely appears to have some relevance beyond endless levels but a question comes to mind as I look this over. Don't you eventually get an overflow of resources if for whatever reason a player manages to go on long enough? Say there was a reason one needed to optimize things? I'm assuming you'd just destroy all the level objects that the player is past at the same time as loading the new ones in?
Code Monkey
Yup exactly, if you were to implement this in a complete game you would definitely clean up the old platforms.
Just cycle through the list and do a simple distance check, if too far, destroy it
Tiziana Minerva
NullReferenceException: Object reference not set to an instance of an object. Any idea why?
Code Monkey
You have something set to null and you're trying to access it
Tiziana Minerva
@Code Monkey I know that the problem is the EndPosition but I don't know how to fix it, already put the EndPosition as the child of LevelStart