• 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
0
Question by soxroxr · Jul 19, 2017 at 11:31 AM · gameobjectsize

Get Size of Gameobject without Mesh or Collider

I'm trying to get the size of a gameobject, but the GO in question is a parent gameobject which is holding multiple child GO's. The parent GO is only there for the purpose of grouping them as one. It doesn't have a mesh renderer or collider. It only has the script that is trying to find the size.

How can I find the size of the group of gameobjects? Without manually creating a collider that encompasses the bounds. At that point, my goal would easier be achieved by manual entry, which I'm deeply trying to avoid as I have a huge variety of objects.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jortd · Jul 19, 2017 at 11:46 AM

What type of object are you trying to get the size of?

Comment
Add comment · Show 2 · 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 soxroxr · Jul 19, 2017 at 11:53 AM 0
Share

Updated question.

avatar image naryk86 · Sep 06, 2017 at 11:07 AM 0
Share

Hello! I'm trying to write a code to instantiate several "pieces" runtime, one next the other. $$anonymous$$y "pieces" are different types of terrain, prefab made of a parent empty game object with prefabs of terrain as children (each of them is create as a combination of several tiles).

If I want to instantiate a "flat terrain" piece (imagine it as it was 5 meter of ground with "Transform " and "BoxCollider2d" components) attached it is all ok...

but when i try to instantiate a prefab made as a sequence of "flat terrain"+"waterfall"+"flat terrain" (where "waterfall it's a prefab of water tiles with only "Transform" component attached) i had the following error: $$anonymous$$issingComponentException: There is no 'Renderer' attached to the "flat terrain" game object, but a script is trying to access it.

The parent prefab (which we can call "flat terrain with a waterfall inside") has only the "Transform" component attached. This is the code I use to calculate bounds of the composite prefab to find the exact location where place the following one

[in Start and Update] Transform o = (Transform)Instantiate(prefab); o.localPosition = nextPosition; prefabBounds = CalcPrefabBounds(o); objWidth = prefabBounds.size.x; nextPosition.x += objWidth;

Where

private Bounds CalcPrefabBounds(Transform obj) {

// First find a center for your bounds. Vector3 center = Vector3.zero;

  foreach (Transform child in obj.transform)
  {
      center += child.gameObject.GetComponent<Renderer>().bounds.center;
  }
  center /= obj.transform.childCount; //center is average center of children

  //Now you have a center, calculate the bounds by creating a zero sized 'Bounds', 
  Bounds bounds = new Bounds(center, Vector3.zero);
  foreach (Transform child in obj.transform)
  {
      bounds.Encapsulate(child.gameObject.GetComponent<Renderer>().bounds);
  }
  return bounds;

}

Could you help me, please?

Thanks in advance

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

94 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

shrinking objects 1 Answer

What is the difference between these, and how do I fix it. 2 Answers

How to create an 3d game object in 300 X 250 size? 1 Answer

UI Button Sizing unit 0 Answers

Can't get Object size to Reset to Original 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