- Home /
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
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!
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?
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?
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"
still don't get it. does your instance does not have a collider or what type are you trying to call .Bounds on?
@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.
Your answer
Follow this Question
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