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
1
Question by homer_3 · Mar 29, 2012 at 02:34 AM · colliderbox collidermodify

How modify a game object's collider?

I'm trying to update a collider's center point to no avail. I'm trying

 void UpdateColliderCenter()
 {
     if(colCenters != null)
     {
         BoxCollider bc = new BoxCollider();
         Bounds b = gameObject.collider.bounds;
         bc.bounds = b;
         
         Destroy(gameObject.collider);
         
         if(colCenters[curIdx].x != 0 &&
            colCenters[curIdx].y != 0 &&
            colCenters[curIdx].z != 0)
         {
             b.center = colCenters[curIdx];
             gameObject.AddComponent(bc);
         }else
         {
             b.center = startColCenter;
             gameObject.AddComponent(bc);
         }
     }    
 }

But I get compiler errors at bc.bound = b; and gameObject.AddComponent(bc); The 1st error is saying that bounds is read only. The other errors state the function only takes strings. Isn't there some way to add a given instance of a component to a game object?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by rutter · Mar 29, 2012 at 03:30 AM

The 1st error is saying that bounds is read only.

It is. You can't edit an objects bounds property directly. Instead, you can edit the object, and its bounds will naturally change depending on what you've done.

For example, if you know the object has a BoxCollider attached, you can adjust its size and/or center point like so:

 var bc = GetComponent(BoxCollider);
 bc.center = Vector3.zero; //move center to local (0,0,0)
 bc.size *= 2;             //double collider's size

The other errors state the function only takes strings.

I'm not familiar with that error. You'll probably get more help if you copy-paste your specific error message in with your question.

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 homer_3 · Mar 29, 2012 at 11:59 AM 0
Share

All the posts I found while googling for this also mentioned setting the size property of a collider, but that is read only as well, even if I cast the collider to a Box Collider like (bc as BoxCollider).size, like some posts I found had suggested, it was still read only. Same goes for size's properties (x and y).

I thought I had been getting a read only error when attempting to modify center as well, but that must have been a different issue, because that no longer errors on me.

What I did finally end up finding out was that I need to modify the extents property of a collider ins$$anonymous$$d of size.

avatar image rutter · Mar 30, 2012 at 04:13 AM 0
Share

Ah, good catch. $$anonymous$$y bad on giving you some questionable information, there. Glad you figured it out, though!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Collider bug? 0 Answers

Getting a sword in place while attacking an enemy 3 Answers

My 2 Objescts doesnt collide 1 Answer

Character Collider doesn't collide with other Colliders 2 Answers

How to detect which side of a box collider was hit? (Top, Bottom, Right, Left) 2D game/ C# 3 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