How do I resize a game object, when it has extended past a boundary?

I have some white blocks, and within the white blocks, I have some cyan-bordered boxes. Whenever the white boxes are extended outside of the cyan bordered boxes, I want to resize them so they are back inside the cyan bordered boxes.

As per my image, they need to remain in their borders, like the fifth one from the top.

How do I resize a game object, when it has extended past a boundary?

Here is my current code:

void Update()
{
if(numOfGeneratedGuideline < numOfGuidelineToGenerate)
{
Generate_Guideline_Positons(numOfGeneratedGuideline);
Generate_Platform_Positions_And_Scale(numOfGeneratedGuideline);
Generate_Platforms(numOfGeneratedGuideline);
numOfGeneratedGuideline++;
}
}

void Generate_Guideline_Positons(int i)
{
float tempGuidelineOffset = groundHeight + (guidelineOffset * (i + 1));

guidelinePosX[i] = worldWidth / 2;
guidelinePosY[i] = tempGuidelineOffset;
guidelinePosZ[i] = 0;
}

void Generate_Platform_Positions_And_Scale(int i)
{
randomGuidelineNumber = Random.Range(1, numOfGuidelineToGenerate + 1);

float tempPlatformPosXMin = (worldWidth - guidelineWidth) / 2;
Debug.Log(tempPlatformPosXMin);
float tempPlatformPosXMax = worldWidth - tempPlatformPosXMin;
Debug.Log(tempPlatformPosXMax);
float tempPlatformPosY = groundHeight + (guidelineOffset * (i + 1));

platformPosX[i] = Random.Range(tempPlatformPosXMin, tempPlatformPosXMax);
platformPosY[i] = tempPlatformPosY;
platformPosZ[i] = 0;

platformScaleX[i] = Random.Range(minPlatformScaleRange, maxPlatformScaleRange);
platformScaleY[i] = 1;
platformScaleZ[i] = 1;

//22 29 36 43 50
}

void Generate_Platforms(int i)
{
GameObject newplatform = Instantiate(platformPrefab,
new Vector3(platformPosX[i], platformPosY[i], platformPosZ[i]),
Quaternion.identity) as GameObject;

newplatform.transform.localScale = new Vector3(platformScaleX[i], platformScaleY[i],
platformScaleZ[i]);
}

• Is it essential for them to change size? or can they be masked? – Savlon Nov 17 '14 at 13:07
• What do you mean by masked ? – JekasG Nov 17 '14 at 22:16
• As in you don't want it to be seen outside of the cyan box... Purely aesthetic... – Savlon Nov 17 '14 at 23:47
• The cyan box is just a Gizmos. It just for debugging. And yes i would like it inside the cyan box – JekasG Nov 18 '14 at 1:36
• Your asking to resize the objects, but I fail to see how that would fix the overlapping, in your example. Do you want the inner object to shrink, so it still moves left, but not outside of the box? Do you want the box to grow, so it still acts as a boundary? It sounds like your asking us how to create a scrollbar, where there is no resizing, but the inside box is restricted from moving outside of the box. – Gnemlock May 5 '17 at 1:22