How can i scale only one direction of a gameobject ?

I want to scale the gameobject only to the left. The way i’m doing it now it’s changing the scale to both left and right.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Test : MonoBehaviour
{

   public GameObject gameObjectToRaise;
   public float raiseAmount;
   public float raiseTotal = 50;
   public float speed = 2;
   public static bool raised = false;

   public int x, z;
   private List<GameObject> cubes;

   public bool randomColor;
   public Color[] colorChoices;

   Vector3 lp;
   Vector3 ls;

   void Start()
   {
       lp = gameObjectToRaise.transform.localPosition;
       ls = gameObjectToRaise.transform.localScale;
       CreateCubes();
   }

   void Update()
   {
       if (DetectPlayer.touched == true)
       {
           if (raiseAmount < raiseTotal)
           {
               float raiseThisFrame = speed * Time.deltaTime;
               if (raiseAmount + raiseThisFrame > raiseTotal)
               {
                   raiseThisFrame = raiseTotal - raiseAmount;
               }

               raiseAmount += raiseThisFrame;

               gameObjectToRaise.transform.localScale += new Vector3(raiseThisFrame, raiseThisFrame, 0);
               cubes[0].transform.localScale += new Vector3(raiseThisFrame, raiseThisFrame, 0);
               cubes[0].transform.position += Vector3.left * (speed / 2) * Time.deltaTime;
           }
           else
           {
               raised = true;
           }
       }
   }

   private List<GameObject> CreateCubes()
   {
       cubes = new List<GameObject>();
       for (int a = 0; a < x; a++)
       {
           for (int b = 0; b < z; b++)
           {
               GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
               if (randomColor)
               {
                   cube.GetComponent<Renderer>().material.color = colorChoices[Random.Range(0, (colorChoices.Length))];
               }
               cube.transform.position = new Vector3(lp.x - 1, lp.y, lp.z);
               cube.transform.Rotate(0, 90, 0);

               cubes.Add(cube);
           }
       }
       return cubes;
   }
}

This is the line where i scale the cube[0]

cubes[0].transform.localScale += new Vector3(raiseThisFrame, raiseThisFrame, 0);

I read that i should create a new empty gameobject and make the cube[0] child of it then to scale the empty gameobject. So i tried but it didn’t work fine.

I added a new global GameObject var called it go

private List<GameObject> CreateCubes()
    {
        cubes = new List<GameObject>();
        for (int a = 0; a < x; a++)
        {
            for (int b = 0; b < z; b++)
            {
                GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                if (randomColor)
                {
                    cube.GetComponent<Renderer>().material.color = colorChoices[Random.Range(0, (colorChoices.Length))];
                }
                cube.transform.position = new Vector3(lp.x - 1, lp.y, lp.z);
                cube.transform.Rotate(0, 90, 0);

                cubes.Add(cube);
            }
            go = new GameObject();
            go.transform.position = new Vector3(lp.x - 1, lp.y, lp.z);
            cubes[0].transform.parent = go.transform;
        }
        return cubes;
    }

Then i tried to scale this empty gameobject
Instead

cubes[0].transform.localScale

I tried

go.transform.localScale

But it’s not working. I tried to do it from this answer:

https://stackoverflow.com/questions/39216454/scale-gameobject-on-just-one-side

But not sure how to make it by script with my code.

I tried to do it this way.
Added global empty new GameObject called it go.
In the bottom in the CreateCubes method i’m setting the new empty GameObject to be in the same position of the cube in cubes[0] position. Then i rotate the go gameobject by 90 degrees like i did with the cubes[0] then i set the cubes[0] to be child of the new empty gameobject.

And in the Update function i’m scaling and moving the go empty gameobject instead the cubes[0]
But still it’s scaling it for both sides and not only to the left.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class RaiseWalls : MonoBehaviour
{

    public GameObject gameObjectToRaise;
    public float raiseAmount;
    public float raiseTotal = 50;
    public float speed = 2;
    public static bool raised = false;

    public int x, z;
    private List<GameObject> cubes;
    private GameObject go;

    public bool randomColor;
    public Color[] colorChoices;

    Vector3 lp;
    Vector3 ls;

    void Start()
    {
        lp = gameObjectToRaise.transform.localPosition;
        ls = gameObjectToRaise.transform.localScale;
        go = new GameObject();
        CreateCubes();
    }

    void Update()
    {
        if (DetectPlayer.touched == true)
        {
            if (raiseAmount < raiseTotal)
            {
                float raiseThisFrame = speed * Time.deltaTime; 
                if (raiseAmount + raiseThisFrame > raiseTotal)
                {
                    raiseThisFrame = raiseTotal - raiseAmount;
                }

                raiseAmount += raiseThisFrame;

                gameObjectToRaise.transform.localScale += new Vector3(raiseThisFrame, raiseThisFrame, 0);
                go.transform.localScale += new Vector3(raiseThisFrame, raiseThisFrame, 0);
                go.transform.position += Vector3.left * (speed / 2) * Time.deltaTime;
            }
            else
            {
                raised = true;
            }
        }
    }

    private List<GameObject> CreateCubes()
    {
        cubes = new List<GameObject>();
        for (int a = 0; a < x; a++)
        {
            for (int b = 0; b < z; b++)
            {
                GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                if (randomColor)
                {
                    cube.GetComponent<Renderer>().material.color = colorChoices[Random.Range(0, (colorChoices.Length))];
                }
                cube.transform.position = new Vector3(lp.x - 1, lp.y, lp.z);
                cube.transform.Rotate(0, 90, 0);

                cubes.Add(cube);
            }
        }

        go.transform.position = cubes[0].transform.position;
        go.transform.Rotate(0, 90, 0);
        cubes[0].transform.parent = go.transform;

        return cubes;
    }
}
  • Add a parent GameObject to your wall / cube
  • Place the parent pivot point (center) at the rightmost point of the wall by moving the wall to that point.
  • Scale the parent object.

Note that the editor should be showing “pivot” and not “center” and “local” instead of “global” when positioning, otherwise the Gizmo will confuse you.