# Unity Renderer.bounds issue?

I would like to make rend.bounds.max.y be always equal to 20 on collision no matter what, using the script below. But it seems that I can't set it manually and the script has no effect on my GameObject.

Script:

public class ClassKey : MonoBehaviour{

void Start()
{
rend = GetComponent<Renderer>();
}

void OnCollisionEnter(Collision col)
{
//more code
rend.bounds.max.y = 20;
//more code
}
}


What I want to achieve: "Change rend.bounds.max.y to 20 when colliding (and keep the new value forever), and let rend.bounds.max.x and rend.bounds.max.z vary freely when colliding"

The problem: Renderer.bounds is read only... How can I go around this?

• Why are you trying to change the bounds? If you explain a little more, people may be able to provide you with an alternative. Nov 25, 2015 at 16:45
• So I can prevent the GameObject from changing its "y" value when colliding, while still being able to change "x" and "z". Nov 25, 2015 at 16:54
• Nov 25, 2015 at 16:58
• I think you going wrong about your problem. You should make a question that is not about that particular script detail - because by definition it's just impossible to assign a value to something that is read-only. You should ask a question about what you are trying to achieve. Like, "I am trying to prevent a gameobject from changing the y value of its bonding box when the given situation happens". Then you show and describe the situation. You will more likely find useful answers. But in any case, be aware that Bounds refer to the AABB, not to the object Y.
– MAnd
Nov 25, 2015 at 17:10
• Also, by your comment in reply to @Savlon, I got thinking: what is the y you want to fix and what are the x and z that you wan to set free to be changed when collision is present? Are these the coord of position, of rotation or of scale?
– MAnd
Nov 25, 2015 at 17:12

Modifying the bounds is impossible for a good reason, which is that the bounds is just an AABB (i.e. non-rotated box) which encloses the entire sprite or mesh. It just shows where, in the form of a box unaffected by rotation, the mesh/sprite reaches. Kind of like a connect the outer dots with lines aligned with the x, y and z axis and is thus just a way of representing it.

Besides, the 'max' you are trying to modify represents the highest coordinate of the bounding box, in other words, generally the right upper corner.

If you intend to actually keep the object at its position, modify transform.position instead.

There's one more thing though: you are probably trying to 'undo' the effect of any collision response. Doing that in this way won't work, because on collision the position is not (necessarily) directly modified. Rather, the velocity and acceleration are modified which in its turn modify the position in subsequent (fixed) updates. If you really want to keep it at y = 20 at all costs, you probably want to try doing this in Update or LateUpdate (the latter makes sure that it is applied after other modifications).

In unity, a vector is implemented as a struct as opposed to a class. One thing this means is that you can not change individual values in a vector by assigning directly as you have done. You need to replace the entire vector with a new one. So:

var old = rend.bounds.max;
rend.bounds.max = new Vector3(old.x, 20, old.z);

• 2 Errors: 1) error CS1612: Cannot modify a value type return value of UnityEngine.Renderer.bounds'. Consider storing the value in a temporary variable Nov 25, 2015 at 16:59
• 2) error CS0200: Property or indexer UnityEngine.Renderer.bounds' cannot be assigned to (it is read only) Nov 25, 2015 at 17:00
• @ManeG. that's what I told when commenting your other question: assigning bounds.max like that is impossible because it's read-only. You will have to that in a different way
– MAnd
Nov 25, 2015 at 17:07
• I can't seem to find a way. Can you write an answer? Thank you. Nov 25, 2015 at 17:10
• @ManeG. as I mentioned in my comments to the question, I don't understand what exactly you are trying to do when you say you want to fix Y and let X and Z vary when colliding. You are talking about position, rotation or scale? Bounds is the AABB only, not position, rotation or scale.
– MAnd
Nov 25, 2015 at 17:37