Infinite Platform Code

Updated on May 15, 2018 in  [R] Scripts
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
8 on May 11, 2018

Hey Brackeys,

I’m new to coding (c#) and unity and I am creating a game somewhat similar to DoodleJump. I watched your video on creating the game, but I want to make the platforms infinite (then destroy after they are used) in order to make the game less laggy and more interesting. Is there any way you could include a code to do this or make a video on it? It would be much appreciated!



  • Liked by
7 on May 11, 2018

What you could do is once the platform exits the screen just destroy it. You could set up a trigger volume on the edge of you screen and once a platform enters it, the platform can be destroyed.


on May 11, 2018

Ok I could definitely do that, I just need a script to randomly generate one the prefabs (platforms) I have created.

on May 11, 2018

Depending on what these platforms need to look like you could just put a ref to a cube prefab and when you instantiate it scale to some random scale. If the platforms look different you could make a list of all the different platform objects and randomly instantiate one of the from the list that are in.

on May 14, 2018

If you could, would you create a c# script for that or just explain to me how to do it? It would be much appreciated.

on May 15, 2018

Yeah I can look into making some kind of prototype tomorrow for you


on May 15, 2018

This a rough prototype I whipped in about 10 minutes. If you have questions let me know.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformGen : MonoBehaviour
    public float m_SpawnDelay;
    private float m_Timer;
    public Transform m_SpawnPoint;
    public float m_PlatformSpeed;
    private List<GameObject> m_Platforms;
    void Start()
        m_Platforms = new List<GameObject>();
    void Update()
        m_Timer += Time.deltaTime;
        if (m_Timer >= m_SpawnDelay)
            m_Timer = 0;
            var newPlatform = GameObject.CreatePrimitive(PrimitiveType.Cube); //Creates a new cube
            //Adds a rigidbody component so we can detect collision with it
            //Disables gravity so it doesn't fall
            newPlatform.GetComponent<Rigidbody>().useGravity = false;
            //Sets the spawn position of the cube
            newPlatform.transform.position = m_SpawnPoint.position;
            //Randomly sets a new scale for the platform.
            //You can modify these values to how you see fit
            newPlatform.transform.localScale = new Vector3(1, Random.Range(1,6), 1);
            //Adds it to the list
        //Loops over all platforms and moves them across the screen
        foreach (var platform in m_Platforms)
            if(platform != null)
                platform.transform.position += Vector3.right * -m_PlatformSpeed;
    void OnTriggerExit(Collider other)
        // Destroy everything that leaves the trigger
        if (m_Platforms.Contains(other.gameObject))


on May 15, 2018

That would be awesome dude. Thanks. If you need more specifics this is what I’m looking for:

Platform prefabs instantiate randomly between two y values and x values (to randomize where they are located on the playing field). When they are no longer in sight, they delete in order to help frame rates. This is intended for Apple/Android devices. 

If you need any more information just let me know.

on May 15, 2018

You have the basic system above. Good Luck

Show more replies
  • Liked by