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 zerophase · May 06, 2014 at 09:28 PM · boxcollider2d

How to add and modify multiple boxcollider2Ds through script?

Hey, I have a character controller, which I'm trying to add and modify multiple BoxCollider2Ds through script. The only thing is I'm not sure how to guarantee I grab the correct BoxCollider2D. I thought I could have a method create a local BoxCollider2D and then add it to the gameobject, but I cannot seem to figure out how to assign the BoxCollider2D. How can I assign a local variable to a gameobject?

Here's my code:

 private void addBoxCollider2D(ref GameObject character, Vector2 size, Vector2 center)
     {
         BoxCollider2D boxCollider2D = new BoxCollider2D();
         boxCollider2D.size = size;
         boxCollider2D.center = center;
         character.AddComponent<BoxCollider2D>();
     }
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
2
Best Answer

Answer by robertbu · May 06, 2014 at 09:36 PM

You never use 'new' with a component. That is if the class is derived from Monobehavior and intended to be used as a component of a game object, you only use AddComponent:

 private void AddBoxCollider2D(ref GameObject character, Vector2 size, Vector2 center)
 {
     BoxCollider2D boxCollider2D = character.AddComponent<BoxCollider2D>();
     boxCollider2D.size = size;
     boxCollider2D.center = center;
 }

By convention, functions and classes start with upper case letters, and variables start with lower case letters.

Comment
Add comment · Show 3 · 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 zerophase · May 06, 2014 at 09:42 PM 0
Share

I thought capitalization was for Public variables and functions, while lowercase was used for private variables and functions?

avatar image robertbu · May 06, 2014 at 09:56 PM 0
Share

Nope. At least not in common usage. The compiler does not enforce these rules. But one benefit of using the rule is that you can routinely do things like:

 Rect rect = new Rect(20,20,100,50);

And if you look around in unity you see this used all over:

  Rigidbody vs rigidbody
  Collider vs collider
  Transform vs transform
  GUITexture vs guiTexture;

and so on. By sticking to the convention, you always know when reading scripts if you are dealing with a class or a variable.

avatar image zerophase · May 07, 2014 at 12:14 AM 0
Share

Yeah, I capitalize classes, and probably internal classes as well.

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

20 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 avatar image avatar image avatar image avatar image

Related Questions

Stacking 2d blocks on each other. 0 Answers

Why is it working in JS but not C# 1 Answer

Does PolygonCollider2D.OverlapPoint() perform differently in Unity player than an Android device? 1 Answer

Unity 5.5 - How to Properly Fix 2D Box Collider Trigger? 4 Answers

Ball bounces at different heights when it shouldn't 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