I'm creating a game in unity for my school project similar to a mobile game called Traffic Racer which is essentially an endless runner. Due to my current scripts, I need to spawn the road at the end of the current road whenever my car passes through the trigger I have made (Instead of just making it look like the road is moving my car is moving forward). The problem is I have no idea how to create a script to spawn it when the current road ends and then delete the previous road. enter image description here

This is what the road looks like and its made up of multiple objects: enter image description here

I placed the trigger just before the end of the road (just before the player can see the end because I have fog.) enter image description here

The game looks like this just in case you're wondering.

Please help, sorry if I made the question hard to read its my first time so I dont know the etiquette of asking questions here.

  • 2
    \$\begingroup\$ What part is not working? Detecting your trigger? Spawning a road? Spawning and aligning the road? Selecting a random piece a road to spawn? Are you getting errors? What code did you wrote? \$\endgroup\$
    – Zibelas
    Commented Jan 3 at 15:08
  • \$\begingroup\$ Detecting the trigger is fine and works but I just don't know how to spawn the road prefab continuously to make an endless road while deleting the roads behind the player for optimal performance. This way the road would be endless until the player loses (By colliding with other objects). \$\endgroup\$
    – menaktee
    Commented Jan 3 at 22:10
  • \$\begingroup\$ Also, I do have a function (in my GameManager script) that runs when the car goes through the trigger but it's just a debug.log statement for now that I used for testing. \$\endgroup\$
    – menaktee
    Commented Jan 3 at 22:21

2 Answers 2


In your hierarchy snippet, the objects are blue, so it appears that you're already using prefabs. Do you know about instantiating prefabs at runtime using scripts? That's the typical way to create new game objects in a scene in Unity.

One thing to be aware of when doing an endless runner: due to floating-point precision issues, if your object's transform position gets too far from the Origin (0, 0, 0), they'll begin to be drawn in distorted ways. The typical way to handle this for an endless runner is to have your runner not move forward at all; instead the environment moves backwards to create the illusion that the runner (or car in this case) is moving forwards. So instantiate your road segment at a set point, start moving it backwards until it's off-screen, then Destroy it.

  • \$\begingroup\$ I was thinking of doing this but I'd much rather prefer to go through the hard route of spawning the prefab continuously and deleting the previous prefabs. Mainly because my game already has too many things that rely on the car moving forward. \$\endgroup\$
    – menaktee
    Commented Jan 3 at 22:13

what i would do is move the road forwards the length between the middle of the lines and the player would not notice

  • 3
    \$\begingroup\$ This answer would be better if it explained in greater detail exactly how to do that. \$\endgroup\$
    – DMGregory
    Commented Jan 4 at 13:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .