- Home /
Bounds is 0 when Instantiated
I have an object with a collider. It is in my scene before I press play. When I press play, I print out its bounds. Those bounds are correct.
If I Load that same object's prefab, instantiate, then print out its bounds, they come up 0. How can I get the bounds of the object's collider without having it in the scene beforehand?
This is for the object which is already in the scene, and it works:
GameObject thing = GameObject.Find("MyThing");
Debug.Log(thing.collider.bounds); //prints out correct bounds
This is the broken one:
GameObject thing = Resources.Load("MyThing") as GameObject;
Debug.Log(thing.collider.bounds);
//results are Center: (0.0, 5.0, 0.0), Extents: (0.0, 0.0, 0.0) which are incorrect.
How can I get the bounds after instantiation?
Are you sure the object in the scene is the exact copy of the prefab and doesn't have any local changes? Try deleting it then readding it to the scene from the prefab and see what happens.
Otherwise you might have some editor scripts changing its values, is this an NGUI object by chance? that one changes the colliders to match sprites and such.
Answer by khanouu · Dec 01, 2014 at 10:37 AM
Hi,
I fixe this issue by using gObj.SetActive( true );
Make sure your game object is active.
From the doc :
Note that this will be an empty bounding box if the collider is disabled or the game object is inactive.
it doesn't work with. but i tried to Instantiate it and deleted it later and it worked
Answer by jmasinteATI · Jul 13, 2016 at 05:53 PM
I noticed that my Extends were 0.0, 0.0, 0.0 and thought something was wrong. The game object was active. The problem was not that my extends were zero. Instead, the log was only showing one decimal place. When I added myGameObject.ToString("F4") the output changed to 0.0201, 0.0256, 0.0257, showing that indeed my extends were not zero, just small.