• 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
2
Question by headkit · Apr 22, 2010 at 10:05 AM · gameobjectinstantiateprefabsizebounds

How can I determine an object of prefab's size (for positioning when instantiating them)

hi all!

new to unity and very exitet! my first question: i want to instatiate objects besides each other and need to know their individual width in the direction they could possible overlap. actually i want to add a variable called "space" for the gap inbetween the positioned objects.

f.e. if i want to position the objects in a row like

function setCards_Row ()
{
    for (var  i=0; i<num_of_Cards; i++) {
        Instantiate (prefab, Vector3(i * spaceBetween, 0, 0), Quaternion.identity);
    }
}

so how do i get the specific width of an gameobject in one direction?

thnx!

edited: if the prefabs are Empty's with children of many objects, is there a way to detect the cild with the biggest Renderer-Bound?

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

2 Replies

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

Answer by duck · Apr 22, 2010 at 10:11 AM

You can use its renderer.bounds. "bounds" is a struct which contains many useful properties such as size, extents, center, min and max.

Note, however, that these sizes will be for the Un-Scaled size of the object (as if its scale was 1,1,1), and the directions (eg, in the size x,y,z values) will be relative to the objects local coordinates.

To position your objects next to each other with a specified gap between, you might modify your code to look something like this:

function setCards_Row ()
{
    var nextPosition = 0;
    var gap = 0.5;
    for (var  i=0; i<num_of_Cards; i++) {
        Instantiate (prefab, Vector3(nextPosition , 0, 0), Quaternion.identity);
        nextPosition  += (prefab.renderer.bounds.size.x + gap);
    }
}
Comment
Add comment · Show 5 · 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 headkit · Apr 22, 2010 at 12:43 PM 0
Share

thank you for your answer! looks quite interesting. but i got the error:

$$anonymous$$issingComponentException: There is no 'Renderer' attached to the game object, but a script is trying to access it. You probably need to add a Renderer to the game object . Or your script needs to check if the component is attached before using it.

avatar image headkit · Apr 22, 2010 at 12:56 PM 0
Share

oh, i see. i instatiate an Empty with meshed objects as children. seems to be impossible in that way. so i will need to ask for one of the objects child renderers. hm...

avatar image headkit · Apr 22, 2010 at 01:09 PM 0
Share

shouldn't it be something like this:

Instantiate (prefab, Vector3(nextPosition , 0, 0), Quaternion.identity); var theRenderer : Renderer = prefab.GetComponentInChildren(Renderer); nextPosition += (theRenderer.bounds.size.x + gap);

(wrong code)

avatar image duck ♦♦ · Apr 22, 2010 at 01:12 PM 0
Share

The 'bounds' struct has an .Encapsulate function, which expands the bounds' properties to include the new point. Using this, you could possibly iterate through every renderer of every child object, and call Encapsulate on the $$anonymous$$ and max point of each child renderer's bounds.

avatar image headkit · Apr 22, 2010 at 01:15 PM 0
Share

goo idea, thank you!

but what about the possibility to find a renderer with prefab.GetComponentInChildren(Renderer); does that work?

avatar image
0

Answer by estani-berlin · May 24 at 08:47 AM

In my case I wanted to draw a gizmo where I needed the prefab size, if present. (You can see in your prefab if there's a Renderer component as mentioned by @duck, in my case it wasn't... (isn't this always the case?). It "might" be after instantiation I guess, but I'm not instantiating it in the Editor, so this wouldn't work for me either)

In any case my prefad had a BoxCollider, using it seems to work quite well, and certainly you don't have to instantiate the prefab to access it:

 prefab.GetComponent<BoxCollider>().size
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

1 Person is following this question.

avatar image

Related Questions

gameObject are not referenced 2 Answers

Instantiate a GameObject from a Prefab without using an original to clone 1 Answer

How do I make prefab follow a gameobject?(C#) 1 Answer

Trouble with destroying an instantiated prefab 2 Answers

Size of a prefab instance is always (0,0,0) 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