Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Get last spawned object's position

Discussion in 'Scripting' started by unity_nVdYtceNQmOPDw, Dec 3, 2019.

  1. unity_nVdYtceNQmOPDw

    unity_nVdYtceNQmOPDw

    Joined:
    Jun 12, 2018
    Posts:
    15
    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.

    Code (CSharp):
    1.  
    2.  void Update()
    3.     {
    4.         randomSpace = Random.Range(minSpawnSpace, maxSpawnSpace);
    5.     }
    6.  
    7. for (int i = 0; i < length; i++)
    8.         {
    9.             randomNumber = PickANumber(-1, 1);
    10.             Vector3 pos = new Vector3(transform.position.x + randomNumber, transform.position.y, i * randomSpace);
    11.             int index = Random.Range(0, objects.Length);
    12.             cube = Instantiate(objects[index], pos, transform.rotation);
    13.             cube.transform.parent = transform;
    14.         }
    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.
     
    Last edited: Dec 3, 2019
  2. Ardenian

    Ardenian

    Joined:
    Dec 7, 2016
    Posts:
    313
    The problem seems to be here:
    Code (CSharp):
    1.  
    2.             int index = Random.Range(0, objects.Length);
    3.             cube = Instantiate(objects[index], pos, transform.rotation);
    4.  
    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).
     
    unity_nVdYtceNQmOPDw likes this.
  3. unity_nVdYtceNQmOPDw

    unity_nVdYtceNQmOPDw

    Joined:
    Jun 12, 2018
    Posts:
    15
    I tried it like this but it's still working the same way:
    Code (CSharp):
    1.             cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    2.             cube.transform.position = pos;
     
  4. Ardenian

    Ardenian

    Joined:
    Dec 7, 2016
    Posts:
    313
    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.
     
    unity_nVdYtceNQmOPDw likes this.
  5. unity_nVdYtceNQmOPDw

    unity_nVdYtceNQmOPDw

    Joined:
    Jun 12, 2018
    Posts:
    15
    Yes of course,thank you for helping.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class SpawnScript : MonoBehaviour
    4. {
    5.     private GameObject cube;
    6.     public int length;
    7.     public float randomSpace;
    8.     float randomNumber;
    9.  
    10.     //-------------
    11.     private float minSpawnSpace = 5f;
    12.     private float maxSpawnSpace = 5f;
    13.  
    14.  
    15.     //random rotation
    16.     //spawn rate
    17.  
    18.     //position and rotation
    19.     private Vector3 positionX;
    20.     private Vector3 positionY;
    21.  
    22.     void Start()
    23.     {
    24.         Spawn();
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.         randomSpace = Random.Range(minSpawnSpace, maxSpawnSpace);
    30.     }
    31.  
    32.     void SpawnX()
    33.     {
    34.  
    35.         for (int i = 0; i < length; i++)
    36.         {
    37.             randomNumber = PickANumber(-1, 1);
    38.             Vector3 pos = new Vector3(transform.position.x + randomNumber, transform.position.y, i * randomSpace);
    39.             cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    40.             cube.transform.position = pos;
    41.             cube.transform.parent = transform;
    42.         }
    43.     }
    44.  
    45.     void SpawnY()
    46.     {
    47.         for (int i = 0; i < length; i++)
    48.         {
    49.             randomNumber = PickANumber(-1, 1);
    50.             Vector3 pos = new Vector3(transform.position.x, transform.position.y + randomNumber, i * randomSpace);
    51.             cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    52.             cube.transform.position = pos;
    53.             cube.transform.parent = transform;
    54.         }
    55.     }
    56.  
    57.     void Spawn()
    58.     {
    59.         SpawnX();
    60.         SpawnY();
    61.     }
    62.  
    63.     int PickANumber(int first, int second)
    64.     {
    65.         int[] numbers = new int[] { first, second };
    66.         int index = Random.Range(0, numbers.Length);
    67.         int result = numbers[index];
    68.         return result;
    69.     }
    70. }
    71.  
     
  6. Ardenian

    Ardenian

    Joined:
    Dec 7, 2016
    Posts:
    313
    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?
     

    Attached Files:

  7. unity_nVdYtceNQmOPDw

    unity_nVdYtceNQmOPDw

    Joined:
    Jun 12, 2018
    Posts:
    15
    This is how it acts when i try it.
    Did you try it with 3d objects?
     

    Attached Files:

  8. Ardenian

    Ardenian

    Joined:
    Dec 7, 2016
    Posts:
    313
    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.
     
  9. unity_nVdYtceNQmOPDw

    unity_nVdYtceNQmOPDw

    Joined:
    Jun 12, 2018
    Posts:
    15
    This is what i mean.
    And i don't understand why it leaves out so much space sometimes.
     

    Attached Files:

  10. Ardenian

    Ardenian

    Joined:
    Dec 7, 2016
    Posts:
    313
    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?
     
  11. unity_nVdYtceNQmOPDw

    unity_nVdYtceNQmOPDw

    Joined:
    Jun 12, 2018
    Posts:
    15
    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.
     
  12. Ardenian

    Ardenian

    Joined:
    Dec 7, 2016
    Posts:
    313
    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:
    Code (CSharp):
    1. GameObject previous = null;
    2. for(int i = 0; i < length; ++i){
    3.  
    4.     // spawn object
    5.  
    6.     if(previous != null){
    7.         Debug.Log(cube.transform.position.z-previous.transform.position.z);
    8.     }
    9.     previous = cube;
    10. }
    To ease your eyes and mind, you could also write:
    Code (CSharp):
    1.         float difference = cube.transform.position.z-previous.transform.position.z;
    2.         Debug.Log(difference >= minSpawnSpace && difference <= maxSpawnSpace);
     
    unity_nVdYtceNQmOPDw likes this.
  13. unity_nVdYtceNQmOPDw

    unity_nVdYtceNQmOPDw

    Joined:
    Jun 12, 2018
    Posts:
    15
    Yes it really did send back the right values.Thank you for replying!
     
  14. Ardenian

    Ardenian

    Joined:
    Dec 7, 2016
    Posts:
    313
    You are welcome! :)
     
    unity_nVdYtceNQmOPDw likes this.