Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Esperento · Jun 08, 2013 at 06:06 PM · collidersizebounds

Matching an object's size

Hi, I'm trying to scale an object (with a rigidbody) to match the exact size of another object (with no rigidbody, just a mesh renderer).

I have tried collider.bounds.size = object2.renderer.bounds.size; which gives me the error CS1612: Cannot modify a value type return value of `UnityEngine.Collider.bounds'. Consider storing the value in a temporary variable

I looked up this error and tried this:

 Vector3 temp = collider.bounds.size;
 temp.x = object2.renderer.bounds.size.x;
 temp.y = object2.renderer.bounds.size.y;
 collider.bounds.size = temp;

but I think I'm doing something stupid here because I still get the same error.

Anyone know how I can do this please?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Jun 09, 2013 at 05:10 AM 0
Share

I don't know about your specific problem, but to get two game object to be the same size:

 transform.localScale = goOther.transform.localScale;
avatar image Esperento · Jun 09, 2013 at 08:48 AM 0
Share

That gives the object the same scale but not the same size - they're both still different heights and lengths as they were different sizes originally.

avatar image xandermacleod · Jun 09, 2013 at 09:47 AM 0
Share

Does it need to be absolutely exact? or can it be off by a few decimal places?

If it doesn't need to be fully exact you should be able to do the following: set one of their localScales to 1 then scale the other and make a note of it's scale values in each axis. These scale values will become your scaleFactors.

Then do something like:

 transform.localScale = new Vector3(other.transform.localScale.x * scaleFactorX, other.transform.localScale.y * scaleFactorY, other.transform.localScale.z * scaleFactorZ);
avatar image Esperento · Jun 12, 2013 at 08:03 AM 0
Share

Great idea but this seems to make the objects disappear entirely. I tried local scale at first and I've now tried render.bounds as it's a 3D text object but his doesn't seem to work either:

transform.localScale = new Vector3(object2.renderer.bounds.size.x scaleFactorX, object2.renderer.bounds.size.y scaleFactorY, object2.renderer.bounds.size.z * scaleFactorZ);

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Immanuel-Scholz · Jun 09, 2013 at 10:12 AM

The error is generated when you try to modify a "value type", which in this particular cas means a struct.

When a function or property return a value types (as structs are), you always get a copy instead of the original. So changing that copy would not make sense, because you want to change the original bounds of the renderer - not just a copy.

The struct that the compiler is complaining about are "bounds", not "size". (Although size is a vector, which is an struct too)

So when you do collider.bounds.size = object2.renderer.bounds.size you are assigning a new size to a copy of the colliders bounds. Which is most definetely not what you would expect. C# emits an error for that.

If at all, you would have to assign the bounds - property itself.

 // doesn't work either
 collider.bounds = new Bounds(collider.bounds.center, object2.renderer.bounds.size);

But then you will get another error, namely that "bounds" is a read-only property.

To change collider sizes, you can either use some specific collider functions (e.g. if you are using a CapsuleCollider, it has some properties as "height", "radius" and "center"). Or you can change the transform - component that the collider is attached to, which is also used by the collider itself.

 // changes the scale of the transform, which is also used by the collider
 transform.localScale = object2.renderer.bounds.size
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Jun 09, 2013 at 10:14 AM 0
Share

Excellent answer. OP -- of course, you can not modify bounds.

avatar image Esperento · Jun 12, 2013 at 07:48 AM 0
Share

Thanks for your detailed answer, Immanuel. This works when I try it on 2 cubes but object2 is a 3D text object and this doesn't scale the cube to match it correctly - it's scaled perfectly in X but in Y is about 3 times too big.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can I use a raycast to retrieve information from the collider? 2 Answers

Changing the size of a text object to only be as long as the letters 0 Answers

Percentage of Collider within a Trigger Area (C#) 1 Answer

Can you scale a Mesh independently of it's Collider? 1 Answer

Converting Bounds.size to GUI? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges