# Why boxCollider bound extents change when changing the size of an gameObject but doesnt in the script?

So, I have two players when I first made them they were with the default size Y = 1. After I finished the scripts I wanted to change their size but when I did that, the calculations use the size of Y = 1 and I don't understand why is that.

The code I am running is this:

public class Player1Controller : PlayersController {
private Vector3 downMost;
private Vector3 upMost;
private float minY, maxY;
private float halfHeight;

// Start is called before the first frame update
private void Start () {
halfHeight = GetComponent<SpriteRenderer> ().sprite.bounds.extents.y;

float distance = transform.position.z - Camera.main.transform.position.z;
downMost = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, distance));
upMost = Camera.main.ViewportToWorldPoint (new Vector3 (1, 1, distance));

minY = downMost.y + halfHeight;
maxY = upMost.y - halfHeight;
}

// Update is called once per frame
private void Update () {
float verticalMovement = Input.GetAxis ("Player1Vertical");
MovePlayer (verticalMovement, transform);
PlayerLimit (transform, minY, maxY);
}
}


The PlayerLimits method is

public void PlayerLimit (Transform transform, float minY, float maxY) {
float restrictY = Mathf.Clamp (transform.position.y, minY, maxY);
transform.position = new Vector3 (transform.position.x, restrictY, transform.position.z);
}


I assume the problem is either in the sprite bounds.extents.y or the restrictY but I don't get it.

This helps me with the Player limits, shown in the next image: As you can see on the left side is the modified player with size less than 1 and on the right side is the player with the default size, the script is the same but the calculations are wrong. The left bar had to go till the end of the border but it doesnt.

• Please post your code as text, not an image, and tag your question correctly. This code is in C#, not the deprecated language UnityScript. Oct 22 '19 at 21:08
• Sorry for the misunderstanding this is my first time asking a question here. Oct 22 '19 at 22:36

TL;DR: if you need to get the right size of the Collider bounds extents, simply scale them accordingly to transform.localScale. Something like:
Vector3 extents = _my_collider.bounds.extents;