# Junior Programming Pathway scaling a cube

In the junior programming pathway for the “mod the cube” assignment, I’m trying to have the cube scale up incrementally by 0.1 until it reaches a certain size, then scale back down by the same increment.

My function for doing this has no errors but the result isn’t what I want.

It starts the cube at 0 and scales up by 0.1 to 9.00001 which is below the lower bound of the range I’ve set and then it gets stuck. I’ve tried changing the value of scaleCube and changing the second if to an else if statement plus a few other things but it still gets stuck at the same number. Could someone help direct me towards making my function work as intended?

``````  {
public MeshRenderer Renderer;
private float scaleCube;

void Start()
{
//default script code is here
}
void Update()
{
scaleCube = cubeScale(scaleCube);
Debug.Log(scaleCube);

transform.localScale = Vector3.one * scaleCube;

transform.Rotate(10.0f * Time.deltaTime, 0.0f, 0.0f);
}
private float cubeScale(float x)
{
float maxRange = 5;
float minRange = 1;
float currentBound = 1;

if (x <= currentBound)
{
x = x + 0.1f;
if (currentBound >= maxRange)
{
currentBound = minRange;
}
}
if (x >= currentBound)
{
x = x - 0.1f;
if (x <= currentBound)
{
currentBound = maxRange;
}
}
return x;
}
}
``````

Your loop is stuck in an infinite loop because as soon as it reaches `1.0f`, it goes to your next part of code which will decrease `x` by `0.1f` then when the code runs again it will increase it by `0.1f` to `1.0f`, but then it reached `currentBound` again so it goes on to your next part of the code which decreases `x` by `0.1f` and so on.

What I’d suggest you having in a case like this is either a `bool` that is checking whether it’s incrementing or not, or a more readable, elegant way - especially if your cube would have another state then increasing and decreasing - is to use an `enum`.

While your `_scaleState` is set to `Increasing` it will increase `x` by the given amount until it reaches your `maxRange` and then it will set `_scaleState` to `Decreasing` which will run the other part of your code that is decreasing `x`. Once `x` reaches `minRange`, `_scaleState` switches back to `Increasing` thus completing a full circle.

I provided you a code below that would fit this scenario:

``````using UnityEngine;

public class CubeScaler : MonoBehaviour
{
public MeshRenderer Renderer;
private float _scaleCube;

enum ScaleState
{
Increasing,
Decreasing
}

private ScaleState _scaleState;

void Start() {
_scaleState = ScaleState.Increasing;
}

void Update()
{
_scaleCube = cubeScale(_scaleCube);
Debug.Log(_scaleCube);

transform.localScale = Vector3.one * _scaleCube;

transform.Rotate(10.0f * Time.deltaTime, 0.0f, 0.0f);
}

private float cubeScale(float x)
{
float maxRange = 5;
float minRange = 1;

if (_scaleState == ScaleState.Increasing) {
if (x >= maxRange) {
_scaleState = ScaleState.Decreasing;
}

x += 0.1f;
}

if (_scaleState == ScaleState.Decreasing) {
if (x <= minRange) {
_scaleState = ScaleState.Increasing;
}

x -= 0.1f;
}

return x;
}
}
``````

This will do ping pong between scale 1 and 5 with descrete steps of 0.1 every 0.2 second (Untested).
You are currently tying to change the scale with 0.1 every frame, which I suspect is not what you want, it will be changing scale to quickly.

``````private float way = 1.0f;
private float timer = 0.0f;
private float cubeScale(float x)
{
timer += Time.deltaTime;
if(timer >= 0.2f) //do scaling change in 0.2S time steps
{
timer = 0.0f;

x += way * 0.1f;
if(x >= 5.0f) { x=5.0f; way *= -1.0f; }
if(x <= 1.0f) { x=1.0f; way *= -1.0f; }
}
return x;
}
``````