Change object size

I have an object called “Paddle” and another object called “Ball”. I want to make a script that if the ball hits an object called “Plus”, “Paddle” will change its size. I currently have this script:

public GameObject pickupEffect;

void OnTriggerEnter(Collider other)
{
	if (other.CompareTag("Ball")) 
	{
		Pickup (other);
	}
}

void OnCollisionEnter(Collision collision)
{
	if (collision.gameObject.name == "Plus") 
	{

	}
}

void Pickup(Collider player)
{
	Destroy (gameObject);
}

Just use paddle.transform.localScale = new vector3(x,y,z);
where x y z are width height and depth

Hi Ranum,

Probably the best way to do this is by adjusting your Paddle GameObject’s transform scale. In order to do that, you’ll need to have a reference to your paddle object in this script. There are lots of ways to do that with varying levels of complexity, but one of the simplest is to have a public GameObject variable called Paddle, and set it to your Paddle object in the unity editor.

All game objects have a transform, and transforms have a local scale vector which can determine how large the game-object is, so a scale of (1, 1, 1) is the default size, a scale of (2, 2, 2) is double size in all three dimensions, a scale of (.5, 1, 2) is half the X-dimension, default y-dimension, double z-dimension, etc. So, to take advantage of that, your code might look like this.

public GameObject paddle; 

private void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject.name == "Plus" )
    {
        paddle.transform.localScale = new Vector3(1.5f, 1.5f, 1.5f); 
    }
}

That will set your paddle to 1.5 times it’s default size. This isn’t really complete code, since it doesn’t include anything that will return the paddle to it’s default size (assuming that’s in your design), and also the size values are just hard-coded, but the syntax is there. You don’t have to create a new vector of course, you could use one that you’ve already got defined somewhere else in your code as well.

I hope that helps, good luck with your game!