Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 northgoingzak · Oct 01, 2018 at 05:10 AM · instantiatesizepositioninginstancinginstantiating

Getting size of instantiated objects.

I am trying to get the width of my instantiated objects so I can use that to position objects of varying size so they are precisely next next to each other without over lapping or gaps. Alternative methods or getting the scale would be of help.

Looking on other threads, I have come across [Bounds.size][1], but it only applies to the empty object that I've applied this script to.

         using UnityEngine;
         using System.Collections;
         
         public class buildingplace : MonoBehaviour {
             public GameObject[] buildings;
             private int[] Instances;
             public Transform newPos;
             public GameObject streetSpace;
             //i= current instance
             private int i;
             //r=current selector
             private int r;
             private float prevObSize=0f;
             private float curObSize;
             private float obDisTraveled;
             void Start ()
             {
                 //randomly select object from array
                 buildings = GameObject.FindGameObjectsWithTag ("building");
                 //--------------------------------------------loop: keep creating buildings until 25 blocks
                 //temp) instantiate randomly selected buildings for number of buildings
                 for (int i = 0; i < buildings.Length * 6; i++) {
                     r = Random.Range (0, buildings.Length);
                     obDisTraveled=newPos.position.z;
                     curObSize = prevObSize;
                     Vector3 boxSize = buildings[i].transform.localScale;
                     curObSize=boxSize.z;
                     newPos.Translate (0, 0, obDisTraveled+prevObSize);
                     Instantiate(buildings[r],newPos.position, newPos.rotation);
                 }
             }
     
     }
 

[1]: https://docs.unity3d.com/ScriptReference/Bounds-size.html

Comment
Add comment · Show 6
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 tormentoarmagedoom · Oct 01, 2018 at 11:41 AM 0
Share

If your instantiated object have 2 empty childs, positioned at the corners of the object/image, they will scale with scale property. You only need to mesure the distance between both points by using Vector3.Distance()

So you will get rhe distnce between the points.

Bye!

avatar image hexagonius · Oct 01, 2018 at 07:29 PM 0
Share

what do you mean when you say Bounds.size ononlypplies to your empty object?
this is not true if you are using a $$anonymous$$eshRenderer as it also has an axis aligned BoundingBox.
could you explain more specifically what the size you try to get is defined with?

avatar image corpsinheretoo · Oct 02, 2018 at 03:12 AM 0
Share

I do not understand this sentence:

Looking on other threads, I have come across [Bounds.size][1], but it only applies to the empty object that I've applied this script to.

Could you rephrase your question? Collider.Bounds.size should give you a Vector3 with the dimension lengths. Is that not working for you?

avatar image northgoingzak · Oct 02, 2018 at 05:08 AM 0
Share

What I mean is I have not been able to use Collider.Bounds.Size for my instantiated objects because instances don't seem to have an attribute ".bounds"

avatar image hexagonius northgoingzak · Oct 02, 2018 at 07:30 PM 0
Share

still don't get it. does your instance does not have a collider or what type are you trying to call .Bounds on?

avatar image northgoingzak · Oct 02, 2018 at 05:19 AM 0
Share

@tormentoarmagedoom your solution seems to require I manually place children objects to set width of every object, but I am looking to get the width automatically so I don't have to do position 3 objects every time I create an object to be instantiated.

0 Replies

· Add your reply
  • Sort: 

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

113 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 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

Need help with instantiating a prefab in c# 2 Answers

Instaniate Prefab 1 Answer

How to limit the instantiation of a prefab/object? 1 Answer

how to make multiplication code simpler. 1 Answer

Prefab instantiated wrong scale on mobile 0 Answers


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