- Home /
Get child position from last gameobject in array
I have a foreach loop that instantiates gameobjects. What im trying to achieve is for the loop to get the position of a child in the last instantiated gameobject and update a variable according to that.
Here's what i've got so far:
foreach(GameObject seam in seams)
{
if (isgenerating) {
Instantiate (seam, genlastpos, Quaternion.identity);
genlastpos = seam.transform.GetChild (0).position;
seamno++;
}
else
{
return;
}
}
The only problem is that i know only how to access the first gameobject.
What would be the most efficient way to do this? Thanks in advance.
Answer by Nido · Apr 01, 2016 at 11:03 AM
Just create a GameObject var to store the "seam" you instantiated. The last loop will store the last GameObject:
private void BlaMethod()
{
GameObject lastGO = new GameObject();
foreach(GameObject seam in seams)
{
if (isgenerating) {
lastGO = (GameObject)Instantiate (seam, genlastpos, Quaternion.identity);
seamno++;
}
else
{
return;
}
genlastpos = lastGO.transform.GetChild (0).position;
}
Or inside the if statement if you need that for each "seam".
Answer by 5c4r3cr0w · Apr 01, 2016 at 02:14 PM
This should work :
genlastpos = seam.transform.GetChild(seams.length -1).position;
Your answer
Follow this Question
Related Questions
How to get values from nested JSON data with foreach or for loop? 1 Answer
How do I compare the position of my player to each elements position of an array of GameObjects? 0 Answers
Monodevelop Debugger evaluates temp variable as "null" in for loop when clearly not null 1 Answer
Foreach Statement Random Sequence/Order? 3 Answers
Function executes only on 1 object 1 Answer