- Home /
Size of a prefab instance is always (0,0,0)
Hi there
I have prepared a simple prefab that represents my player character. I have instantiated this prefab and now want to find out the position and height of the instance via a C# script. Determining the position works fine (In the following snippets "_character" is my local variable, holding the game object = instantiated prefab):
Debug.Log("Position: " + _character.transform.position);
Displays correctly as:
Position: (2.2, 0.0, -3.0)
But it won't work for the size of the instance:
Debug.Log("Size: " + _character.renderer.bounds.size);
Displays as:
Size: (0.0, 0.0, 0.0)
The instance has a component "Mesh Renderer" attached to it.
What am I doing wrong?
This isn't exactly an answer, but if the object has a collider - http://docs.unity3d.com/Documentation/ScriptReference/Collider-bounds.html ?
collider.bounds.size
Hi ShadoX. Thanks a lot for your comment (-:
Yep, I have indeed a collider attached. And "collider.bounds.size" works just fine, displaying the correct values. I'm only struggling with "renderer.bounds.size" which keeps displaying (0,0,0).
I guess it's not set until the item is rendered? Perhaps use the mesh bounds and offset them to the position?
You're a genius, whydoidoit, your guess is absolutely right. I tried to deter$$anonymous$$e the size whilst Unity has not yet completely finished rendering the game object. Thank you so much (-:
Answer by Parallaxe · Oct 17, 2013 at 01:13 PM
Hi there
"whydoidoit" pointed me in the right direction, his guess was absolutely correct:
Initially I had tried to determine the size at a point in time, where Unity did not yet have completely finished rendering the respective game object. Moving the whole thing to another code position, where I was sure that Unity had finished rendering all game objects, did the trick.
Thanks a lot to "whydoidoit"! Question has been answered.
Your answer
Follow this Question
Related Questions
How to set RectTransform size relative to the size of the textbox? 1 Answer
How can I determine an object of prefab's size (for positioning when instantiating them) 2 Answers
Prefabs with sprites, make them all same size 0 Answers
Determining the size of a prefab 0 Answers
Array of prefabs with different sizes stick to each other 0 Answers