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
4
Question by maggot · Nov 11, 2011 at 11:01 AM · c#colliderboxadding

Adding a box collider to an object in csharp script

How do I add a box collider to an object in csharp script ? using this code for instance :

         level001cube001.AddComponent("BoxCollider");

Does not give me access to the center property that I want.

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

3 Replies

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

Answer by green_core · Nov 11, 2011 at 01:03 PM

You can add any components to a gameobject by using a function GameObject.AddComponent()

There are two versions of this function: generic and not.

 //Returns object that you can cast to BoxCollider
 var boxCollider1 = (BoxCollider) level001cube001.AddComponent("BoxCollider");
 
 //Returns BoxCollider
 var boxCollider2 = level001cube001.AddComponent<BoxCollider>();

So, to have access to any properties of BoxCollider class you should use generic version of the function or cast to BoxCollider a result of non-generic version.

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 maggot · Nov 11, 2011 at 05:00 PM 0
Share

Ah, I'm adding a primitive of type cube, and box collider is already included as a component. Its just called collider, so I access it through level001cube001.collider I tested it through disabling the enabled value with script, and that worked so I'm thinking that it is focused on the boxcollider component. I still can't access the center or scale properties though.

I got this code to work :

var boxCollider1 = (BoxCollider) level001cube001.AddComponent("BoxCollider");

so I can access the center property like so : boxcollider1.center = new Vector(2,3,4);

but when I run the game I get the error Object reference not set to an instance of an object

avatar image green_core · Nov 11, 2011 at 06:40 PM 0
Share

GameObject.collider returns object of type Collider that is base class of all colliders. But it has not a "center" property . To access to this property you should cast object to a proper type.

GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); var boxCollider = (BoxCollider)cube.collider;

$$anonymous$$eep in $$anonymous$$d, if collider type of the object is not BoxCollider this code throw InvalidCastException. It may happens if you create a primitive that different from Cube.

About your code. This works at my machine: var boxCollider1 = (BoxCollider) gameObject.AddComponent("BoxCollider"); boxCollider1.center = new Vector3(2,3,4); //O$$anonymous$$

avatar image maggot · Nov 11, 2011 at 07:36 PM 0
Share

Alright! I got it working, I had this line which wasn't needed :

GameObject level001cube001 = new GameObject();

then GameObject was missing from :

GameObject level001cube001 = GameObject.CreatePrimitive(PrimitiveType.Cube);

so it was :

level001cube001 = GameObject.CreatePrimitive(PrimitiveType.Cube);

So my solution is this :

     GameObject level001cube001 = GameObject.CreatePrimitive(PrimitiveType.Cube);
     var boxCollider = (BoxCollider)level001cube001.collider;
     boxCollider.center = new Vector3(2,3,4);
avatar image
3

Answer by OregonOx · Nov 01, 2012 at 04:44 PM

"In C# Script" as requested:

 BoxCollider _bc = (BoxCollider)level001cube001.gameObject.AddComponent(typeof(BoxCollider));
 _bc.center = Vector3.zero;
Comment
Add comment · Show 1 · 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 shaibam · Jul 08, 2015 at 12:04 PM 0
Share
 A way of doing this is without code.
 Create a sprite, with animations.
 add to it every Component that you will need along the way.
 than open the "Animation" window Press "Add Property" and than you can add and control the behavior of all components on every fame.
 
 eg. lets say that the gravity should only start when the sprites gets to its final frame.
 
 Select "Add Property -> RigidBody2d ->is $$anonymous$$inematic 
 and take the last key-frame back one frame.
 which means that as long as the animation didn't enter its final frame the Sprite will not fall, and only on the last frame the gravity will start to affect the sprite.`enter code here`
 
avatar image
2

Answer by shaibam · Jul 08, 2015 at 12:13 PM

 A way of doing this is without code.
  Create a sprite, with animations.
  add to it every Component that you will need along the way.
  than open the "Animation" window Press "Add Property" and than you can add and control the behavior of all components on every fame.
  
  eg. lets say that the gravity should only start when the sprites gets to its final frame.
  
  Select "Add Property -> RigidBody2d ->is Kinematic 
  and take the last key-frame back one frame.
  which means that as long as the animation didn't enter its final frame the Sprite will not fall, and only on the last frame the gravity will start to affect the sprite.`enter code here`
  
Comment
Add comment · 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

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

Add an animation clip with AnimationClip using cSharp script 1 Answer

how to resize box collider? 4 Answers

Internal collisions 1 Answer

animation and sound on collide 1 Answer

I keep getting CS8025 parsing error 1 Answer


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