- Home /
Set bounds of collider added at runtime
I am trying to add the bounds of a collider that is added at runtime. For some reason, I am unable to see updates to center or size or extent when I set it using the below:
var p = new GameObject("box"); p.AddComponent(BoxCollider); p.collider.bounds.center = Vector3(4.5,4.5,4.5);
. . .
The resulting collider shows up in the inspector, but the values are always default, i.e., center and size aren't showing up as defined.
How do you set the bounds, center, and such of a collider added at runtime?
Answer by simubrett · Mar 13, 2012 at 04:25 AM
I don't think you can assign the bounds of a collider directly. However, the collider itself has center and extents properties that you can assign. As an example:
GameObject go = new GameObject();
BoxCollider collider = go.AddComponent<BoxCollider>();
collider.center = new Vector3(2, 2, 2);
collider.extents = new Vector3(9, 12, 18);
it looks like not calling it collider.bounds.center etc does the trick. thanks!
in unity js... you actually have to call go.GetComponent(BoxCollider).center
... etc
Your answer
Follow this Question
Related Questions
How can I check if an instantiated object collides with another instantiated object? 1 Answer
How can I add the OnTriggerEnter function to all game objects that I instantiate? 1 Answer
How to adjust two colliders really close to each other w/o them colliding? 1 Answer
checking collisions before instantiating (not Physics.OverlapSphere) 2 Answers
Making the collider change after being instantiate 2 Answers