How to find the width and height of an object

Was wondering how do you find the width and height of an object as am trying to come up with a way to allow an object to wrap around the screen. For an example once the object has gone completely off screen to the right it will then come back on screen on the left and vice versa. I want it to be able to do this no matter the screen size but can’t seem to figure out how to get the object’s width. Any help will be appreciated.

Use the Bounds on the Renderer. So a script like the following would work…

Bounds  myBounds = renderer.bounds;
Vector3 width = myBounds.width;
Vector3 height = myBounds.height;

Can also use min / max positions with myBounds.min.x/y and myBounds.max.x/y

Thanks I also came across another issue of just trying to change the x position of the object as unable to use something like this:

myTransform.position.x -= Screen.width;

It says I can’t do that as it isn’t a variable. So will the only way to change the x position is to create a new Vector3 and give variables to each axis?

Also will what you say to use go in my Update method? as can’t create it outside a method. Also there doesn’t seem to be a width/height for myBounds either.

Correct.

Vector3 pos = transform.position;
pos.x -= Screen.width;
transform.position = pos;

Though if you are using the Screen.width, you’ll need to transform that into world-space first. You’re better off using a function that checks to see if the object is in view of the camera, such as…

Vector2 viewCoords = targetCamera.WorldToViewportPoint(object.transform.position);
if(viewCoords.x < 0f || viewCoords.x > 1f)
{
   // Object is not horizontally on the screen
}

Thanks for helping again. I noticed though that targetCamera doesn’t exist anymore as did not find it in the scripting reference and believe it has been replaced with camera. Please correct if I’m wrong about this.

targetCamera is just a generic carabaos I typed referring to the camera you need the object to stay within.