- Home /
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.
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.
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
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$$
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);
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;
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`
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`
Your answer
Follow this Question
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