Get last spawned object's position

Hi!
I’m using a for loop to spawn objects,and i would like to have x amount of space between the spawned objects.
But if i do it like this,it starts to make the given space bigger and bigger.

 void Update()
    {
        randomSpace = Random.Range(minSpawnSpace, maxSpawnSpace);
    }

for (int i = 0; i < length; i++)
        {
            randomNumber = PickANumber(-1, 1);
            Vector3 pos = new Vector3(transform.position.x + randomNumber, transform.position.y, i * randomSpace);
            int index = Random.Range(0, objects.Length);
            cube = Instantiate(objects[index], pos, transform.rotation);
            cube.transform.parent = transform;
        }

I think it’s because when the “i” variable starts to get bigger the gap between the objects multiplying in a wrong way.
So i think if i could get the last spawned object’s position and added the random space to it’s transform.position.z,it would work but i don’t know how to do it.
Thanks in advance.

The problem seems to be here:

            int index = Random.Range(0, objects.Length);
            cube = Instantiate(objects[index], pos, transform.rotation);

You use an object that already has an offset as a base for instantiating another object. I wouldn’t be surprised if the instantiated object has the position pos + objects[index].transform.position.

Use a general prefab object instead. See also Prefabs (Unity Manual).

1 Like

I tried it like this but it’s still working the same way:

            cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
            cube.transform.position = pos;

Hm, I see, could you post the whole code, please? It is not very clear in your initial code what is what.

I had that weird exponentially increasing offsets in the past, too, I am confident that at some point, you use a transform of an object that already has an offset. If you post the whole code, I can run some quick tests myself.

1 Like

Yes of course,thank you for helping.

using UnityEngine;

public class SpawnScript : MonoBehaviour
{
    private GameObject cube;
    public int length;
    public float randomSpace;
    float randomNumber;

    //-------------
    private float minSpawnSpace = 5f;
    private float maxSpawnSpace = 5f;


    //random rotation
    //spawn rate

    //position and rotation
    private Vector3 positionX;
    private Vector3 positionY;

    void Start()
    {
        Spawn();
    }

    void Update()
    {
        randomSpace = Random.Range(minSpawnSpace, maxSpawnSpace);
    }

    void SpawnX()
    {

        for (int i = 0; i < length; i++)
        {
            randomNumber = PickANumber(-1, 1);
            Vector3 pos = new Vector3(transform.position.x + randomNumber, transform.position.y, i * randomSpace);
            cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
            cube.transform.position = pos;
            cube.transform.parent = transform;
        }
    }

    void SpawnY()
    {
        for (int i = 0; i < length; i++)
        {
            randomNumber = PickANumber(-1, 1);
            Vector3 pos = new Vector3(transform.position.x, transform.position.y + randomNumber, i * randomSpace);
            cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
            cube.transform.position = pos;
            cube.transform.parent = transform;
        }
    }

    void Spawn()
    {
        SpawnX();
        SpawnY();
    }

    int PickANumber(int first, int second)
    {
        int[] numbers = new int[] { first, second };
        int index = Random.Range(0, numbers.Length);
        int result = numbers[index];
        return result;
    }
}

Thanks, I used it and it doesn’t appear to increase the space, see the attachment. How does it look for you? I used a length of 100 with a space of 5. Even with setting minSpawnSpace and maxSpawnSpace to different values, to a range, didn’t change that.

What Unity version are you using?

5241209--523223--SpawningSpaceOffset.png

This is how it acts when i try it.
Did you try it with 3d objects?

5241230--523241--Képkivágás.PNG

Except for that red thingy, it does look exactly like mine. How should it look like?

PS: draw.io is a nice website for quickly drawing a sketch or construct a shape.

This is what i mean.
And i don’t understand why it leaves out so much space sometimes.

5241239--523244--sdfsdfgdsgpng.png

Sorry, I genuinely don’t get it.

With your spawn script, you create an array of two objects, every time with one being slightly off in x and the other one being slightly off in y. Between two of these cubes and two other ones, there is a random space in z direction. This random space is at least minSpawnSpace and no larger than maxSpawnSpace.

However, when I look at the screenshot that you posted, I see exactly that. I don’t understand why you see the right space to the left and right and a supposedly huge amount of space in the middle.

Do you not want cubes sharing the same x and y coordinates to be too far off from each other?

No the offsets on the x and y are good,but it seems to me that it’s leaving larger amount of spaces on the z axis than the maxSpawnSpace.But sometimes,as i drew on the picture,the spaces are the riight amount but sometimes it’s not.At least it’s seems like it.

You could log the difference in z between an previously spawned object and a just spawned object to see if it is true.
It could look something like this:

GameObject previous = null;
for(int i = 0; i < length; ++i){
 
    // spawn object

    if(previous != null){
        Debug.Log(cube.transform.position.z-previous.transform.position.z);
    }
    previous = cube;
}

To ease your eyes and mind, you could also write:

        float difference = cube.transform.position.z-previous.transform.position.z;
        Debug.Log(difference >= minSpawnSpace && difference <= maxSpawnSpace);
1 Like

Yes it really did send back the right values.Thank you for replying!

You are welcome! :slight_smile:

1 Like