SOLVED! - If New Int = Spawn Row Problem

I’m making a puzzle game that spawns rows as the camera moves down. I have every other part of the code running fine, I’m just having difficulty wrapping my head around this problem.

Summary:

As the camera moves, I have a variable that is checking the position at 5, 4, 3, 2, 1, 0, -1 and so on. When the variable has changed, I’d like to spawn a row of puzzle pieces at that location once, so it builds layers underneath over time.

Code:

I’m currently checking the position of the camera as it moves in the y direction as such:

mainPosition = Mathf.RoundToInt(this.transform.root.position.y);

Questions:

I want to check when the int has updated and then spawn the objects. I just have no idea how to do these things:

  1. Check when the integer has changed
  2. Spawn a row once and only once when the integer has changed

I’m currently working in UnityScript. Thanks!

Found a solution:

I know the y location where I want to spawn the rows and I know that each row is -1 of the previous one. With these knowns, I experimented with a Invoke Repeating function as such.

function Start()
{
	InvokeRepeating("TimerLoop",2,(1/gameManagerScript.scrollSpeed));
}

The TimerLoop is the spawn sequence function that calculates the grid positions, spawns the objects Y-1 from the previous, and checks for redundant puzzle objects.

The function starts after 2 seconds, but the neat part is how the function repeats. It’s based on the speed in which the camera scrolls, so if the camera is fast (higher difficulty), the spawn rate will keep up with it and vice versa.

Next on the list is controlling when the invoke starts in relation to the game starting and fixing some other issues that have cropped up as the game spawns objects.