Runner tutorial

Hello everyone

This is my first post so I apologize if my problem is unclear. I’m following this tutorial Runner, a Unity C# Tutorial
and I just wanted to add spacing between the cubes so i made two spacing var…one normal and one big… The problem i’m having is once the first cube is dequeued and requeued at the end of the line it still has normal spacing for some reason but every cube after that has the big spacing…I’ve found out that changing the recycleOffset to 0 fixing this but then the first cube is dequeued soon as my play reaches it.

Can someone help explain to me what’s going wrong? Sadly I am NO programmer.:frowning:

public class Skyline : MonoBehaviour 
{
	
	public Transform prefab;
	public int numberOfObjects;
	public float recycleOffset;
	
	public float space;
	public float bigSpace;

	private Vector3 nextPosition;
	private Queue<Transform> objectQueue;

	void Start () 
	{	
		objectQueue = new Queue<Transform>(numberOfObjects);
		nextPosition = transform.localPosition;
		for(int i = 0; i < numberOfObjects; i++)
		{
			Setup();
		}
	}
	
	void Update () 
	{
		if(objectQueue.Peek().localPosition.x + recycleOffset <= Runner.distanceTraveled)
		{
			Recycle ();
		}
	}
	
	void Setup()
	{
		Transform o = (Transform)Instantiate(prefab);
		o.localPosition = nextPosition;
		nextPosition.x += o.localScale.x + space;
		objectQueue.Enqueue(o);
	}
	
	void Recycle()
	{
		Transform o = objectQueue.Dequeue();
		o.localPosition = nextPosition;
		nextPosition.x += o.localScale.x + bigSpace;
		objectQueue.Enqueue(o);	
	}
}

Thank You

[edit] Code tags added by admin. Please use code tags properly.

Firstly, your Setup() function will only ever run once, as it’s triggered through Start() which runs when you start up the game, so the variable named “space” will only ever come into play that one time immediately after starting up.

Secondly, assuming it’s the blocks in the skyline you want spaced out, you could do something like this in the Recyle() function, which will randomize the space between each block between 0 (minimum) and 8 (maximum):
nextPosition.x += o.localScale.x + Random.Range( 0, 8 ); (note: you can replace the specific values 0 and 8 with int or float variables)

The “recycleOffset” variable is used to determine when objects should be dequeued requeued (based on how far your player has ran), so you should probably reset that to it’s original value. :slight_smile:

Thanks Xoduz for the feedback, I still have much learn…I think i did figure out the reason that causes my first object is placed incorrectly is because
of my nextPosition var… it seems to be holding on to it’s position stored from the for loop…“I did Debug.Log to see” So when i set the first o.localPosition = nextPosition in the Recycle() the first run through sets it right behind the previous…that’s what i’m thinking anyway, BUT i’m not even a programmer. What do you think?