- Home /
How can I calculate the "total length" of a series of instantiated gameobjects?
I am trying to calculare at each Update() the total length (as y scale) of a series of instantiated object. In my case, the script scales a cylinder along the y axis, then at a certain y-scale value, a new cylynder is instantiated as child of the previous one. How can I calculate the total length as sum of y-scale of each cylinder? Also, I need to store this total length each update in a variable because I need it to do some other calculations.
For instance, at 3 instantiated cylinders, my situation is like this one in the image:
each gameobjectMESH has y-scale that I need, while the containers (the gameobjects without "mesh") have not (y-scale = 1).
So, to summarize, I need to sum each y-scale value of every gameobjectMESH at each Update().
Thank you!
Answer by jmhoubre · Jul 20, 2020 at 08:55 PM
Hello,
You need to browse the tree structure of your Seed object.
For that, you can use Transform.childCount which gives the number of children of the object, and the method Transform.GetChild (int idx), which returns the transform of the child of index idx.
To test whether the length of the object should be taken into account or not, I suggest that you tag the objects to be counted with for example "Cylinder". You will test the different objects with the GameObject.CompareTag method.
Good luck (and sorry for any mistakes in English).
I don't get how I could take the y-scale of each gameobject and sum them.
I count how many children I have at beginning with Transform.childCount, then I cycle through each of them with Transform.GetChild, and then what?
Sorry, newbie here...
Hello, here is a possibility to browse your structure. I have put in a lot of debugging so you can fix it if I haven't grasped your problem. This script is to be attached to your Seed object. Note that I only take localscale into account.
using UnityEngine;
public class TotalLength : $$anonymous$$onoBehaviour {
// [SerializeField] to see the values in the inspector.
[SerializeField] private float m_TotalLength;
private void Start () {
// Initialization.
m_TotalLength = 0f;
// Call the recursive method with the root.
Add (transform);
}
private void Add(Transform _transform) {
print ("Work on " + _transform.name + " of Y size of " + _transform.localScale.y);
// Is the current object a container or a cylinder ?
// there are several ways to do this test, I chose one of the simplest, but not optimized.
if (_transform.GetComponent<$$anonymous$$eshRenderer>()== null) {
// No mesh : it's a container.
print (_transform.name + " is a container.");
print (_transform.name + " has " + _transform.childCount + " child/children.");
if (_transform.childCount==0) {
// No children : no action.
print (_transform.name + " has no children.");
return;
} else {
// Children : browse the children.
print ("Work on " + _transform.name + " children");
for (int index = 0; index < _transform.childCount; index++) {
print ("Work on " + _transform.GetChild (index).name);
Add (_transform.GetChild (index));
}
}
} else {
// There's a mesh : it's a cylinder.
print (_transform.name + " is a cylinder.");
print (_transform.name + " has " + _transform.childCount + " child/children.");
// Update length.
print ("Add " + _transform.localScale.y);
m_TotalLength += _transform.localScale.y;
print ("New length = " + m_TotalLength);
if (_transform.childCount == 0) {
// No children : no action.
print (_transform.name + " has no children.");
return;
} else {
// Children : browse the children.
print ("Work on " + _transform.name + " children");
for (int index = 0; index < _transform.childCount; index++) {
print ("Work on " + _transform.GetChild (index).name);
Add (_transform.GetChild (index));
}
}
}
}
}
Good luck (and sorry for any mistakes in English).
Thanks, I've tried to add your script to Seed container. It runs but does not calculate the total length:
The total length in the inspector is 0 even with children.
The Debug.Log returns 0 in length and no children (that's wrong as you can see from the Hierarchy in the image above)
The meshes children of Seed container have the correct y scale value but is not counted:
Probably because the scale is not local? I don't know... Thanks for the answer!
Strange... It works perfectly with me ... No matter how much I reread the code, I don't see the origin of the problem ...
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Character Model - Selecting Submeshes via a Script 1 Answer
How can I move an object a certain distance, once? 2 Answers
why is transform.position giving me local location?(c#) 0 Answers