Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question regular spacing of gameobjects

Discussion in 'Scripting' started by julien83franceschi, Mar 12, 2023.

  1. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    226
    Hello to all,

    I would like to align and space a capsule, a cube, a rectangle (composed of several cubes horizontally) a sphere or a cylinder.
    For this I use a 1-dimensional array of prefabs and I store the list of my GameObjects (components I want to space) in it.

    I calculate the length of a GameObject, I calculate its position :
    position[i + 1] = position + length + space;

    Then, I create the components with Instantiate :
    GameObject instance = Instantiate(prefabs, transform.position, Quaternion.identity);

    Here is the code I do:

    Code (CSharp):
    1. void Start()
    2.     {
    3.         int longueur = 0;
    4.              
    5.  
    6.         grille[1, 2] = 1;
    7.         grille[2, 1] = 1;
    8.         grille[2, 2] = 1;
    9.         grille[2, 3] = 1;
    10.         grille[3, 2] = 1;
    11.  
    12.         for (int i = 0; i < 5; i++)
    13.             if (grille[0, i] == 1)
    14.             {
    15.                 longueur++;
    16.             }
    17.  
    18.         Debug.Log(longueur);
    19.         position[1] = 0;
    20.         for (int i = 1; i < prefabs.Length; i++)
    21.         {
    22.             position[i + 1] = position[i] + longueur + space;
    23.         }
    24.        
    25.         for (int i = 0; i < prefabs.Length; i++)
    26.         {
    27.             GameObject instance = Instantiate(prefabs[i], transform.position, Quaternion.identity);
    28.             instance.transform.position += new Vector3(i * 2, 0, 0);                  
    29.  
    30.         }
    31.     }
    The problem is that nothing is displayed on the screen.
    If you can look at my code and correct it so that I can see the spacing of the various objects on the X axis.

    Thanks to you,

    A+
     
    Last edited: Mar 12, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Just debugging! Don't stop with one lousy line 18 output!

    Get busy and put Debug.Log() statements inside your loop, prove the prefabs.Length, etc.
     
  3. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    You're instantiating your objects at the position of the object the script is on by putting them at transform.position. Then you're using += on the objects to add the new position onto the current position. If your script happens to be on a camera that's at -10 z, for instance, your objects are all going to be at -10 z and not show up on the screen. But they'll be in the scene and spaced apart by 2 units each at -10 z. Also, you're not using all those spacing calculations you're making in the final position. By using i * 2 you're just spacing them each 2 units apart.

    Assuming your script is on a camera at the default z position of -10:
    Code (CSharp):
    1. instance.transform.position += new Vector3(i * 2, 0, 0);
    2.  
    3. //should be
    4. instance.transform.position = new Vector3(i * 2, 0, 0);
     
  4. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    226
    Hello,

    I have corrected the += to = at the end of the code.
    I tried to set the Z axis to -10 (X and Y = 0).

    Still nothing is displayed on the screen.

    Is it good to use the transform.position statement in the following code:
    I have tried to remplace transform.position by position but the debbuger show an error.

    Code (CSharp):
    1. GameObject instance = Instantiate(prefabs[i], transform.position, Quaternion.identity);
    Thanks for your help,

    A+
     
  5. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    Yeah you can do that. Just be aware that when you do, it's going to put the position of the object at the position of the transform the script is on. That's why I guessed you might have it on a camera at its default position, as the result of that would be that your objects would all be at -10z and you wouldn't be able to see them in the game view at runtime.
     
  6. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    226
    Hello,

    in my case the instruction position returns an error message.
    It displays the following message:

    impossible to convert 'int' to 'UnityEngine.Vector3'.


    I tried to set my camera to Z = -10.
    But still no GameObject in sight.
    I fiddled with the position variable (with high or low or negative values),
    but still nothing visible.
    Thanks to you,

    A+
     
    Last edited: Mar 12, 2023
  7. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    You don't need to set your cameras z position to -10. I only suggested that perhaps your script is on a camera that is at the z position -10 and, if so, your objects would be spawning at that z position and thus not be visible to the camera.

    Have you paused the scene during play mode and gone back to the scene window and looked around to see where your objects are and what their transform position numbers are?

    Also, though it does not apply to your actual issue, to properly set a camera to a z of -10 you would need to set its entire vector3, not just the z position. You're getting that error because you're trying to convert an int of -10 into a vector3 transform position.
     
  8. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    226
    Bonjour à tous,

    Toujours pas de GameObjects en vue.
    J'ai tout essayé mais mon code ne semble pas fonctionner.
    J'ai essayé d'attribuer le code à la caméra mais sans succès.

    En attendant une aide précieuse,
     
  9. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    I think you're still misunderstanding me. Putting the script on the camera is not necessary. That was just an assumption I made based on your results. As Kurt suggested, you should do some proper debugging to determine where your problem is coming from.

    For example, since you can't see your objects:
    - Make sure your objects are actually spawning.
    - Make sure your objects mesh renderers are turned on.
    - Make sure your objects transform positions are in the proper spots to be visible.
    - Make sure your objects have a scale big enough to see.
    And so on until you discover the source of the problem.

    Also, it would be helpful if you posted in english rather than french.
     
  10. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    226
    I can't make sure that the object mesh renderings are enabled, because the objects are created dynamically with code.

    I am not sure how to change the scale with code I think it is (X =1, Y =1, Z=1).

    I have modified the variable position with different values (low, high, negative) in order to center my GameObjects in the scene, but no traces of Cubes, cylinder caps or anything else :

    My GameObjects are created in a 1 dimensional array, called prefabs (see below).
    And are modifiable in the Inspector tab.
    Code (CSharp):
    1. GameObjects [] préfabs
    I can change the camera settings but nothing appears on the screen.

    Thanks for your help,

    A+
     
    Last edited: Mar 13, 2023
  11. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    226
    Hello to all,

    I have reviewed my code, I have well touched up, well purified and here is what it gives:
    Code (CSharp):
    1. public class gpt : MonoBehaviour
    2. {
    3.     float length = 4;
    4.     float space = 3f;
    5.     int numGameObjects = 4;
    6.     public GameObject[] objectsToSpace;
    7.     Vector3[] positions = new Vector3[4];
    8.     int [,] grid;
    9.  
    10. void Start()
    11.   {
    12.       int length = 0;
    13.  
    14.       grid[1, 2] = 1;
    15.       grid[2, 1] = 1;
    16.       grid[2, 2] = 1;
    17.       grid[2, 3] = 1;
    18.       grid[3, 2] = 1;
    19.  
    20.       for (int i = 0; i < 5; i++)
    21.           if (grid[0, i] == 1)
    22.           {
    23.               length++;
    24.               Debug.Log(length);
    25.           }
    26.     }
    27.  
    28.     private void Update()
    29.     {
    30.         positions[0] = new Vector3(20, 0, -10);
    31.  
    32.         for (int i = 0; i < numGameObjects - 1; i++)
    33.         {
    34.             positions[i + 1] = positions[i] + new Vector3(length + space, 0, 0);
    35.         }
    36.  
    37.         for (int i = 0; i < numGameObjects; i++)
    38.         {
    39.             objectsToSpace[i].transform.position = positions[i];
    40.         }
    41.     }
    42. }
    The problem is that nothing appears in the scene.
    I can manipulate the camera and the array positions[0] = new Vector3(20, 0, -10) (for example) but nothing appears;

    I specify that the code is attached to an empty (see below picture).

    I also specify that the Debug.Log () does not display anything.
    I tried other codes to space 4 GameObjects between them with a regular gap, but same, no visual results.
    If you want to try or tweak my code or the camera, or if you can send me the parameters of a camera where the GamesObjects would be visibles..

    you are welcome.
     
    Last edited: Mar 14, 2023
  12. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    Your issue is coming from line 30 where you're starting the spacing at a vector3 of (20, 0, -10). Assuming your camera is at a default vector3 of (0, 0, -10), starting your spacing at the currently defined vector3 of (20, 0, -10) results in your objects being spaced 20 units to the right of the camera and on the same z plane as the camera, thus invisible to the cameras view. This is easily seen by pausing the game and looking at the position of the objects in the scene view after the spacing code has run.

    If you set the vector3 on line 30 to vector3.zero, or (0, 0, 0), your spacing starts at origin and your objects, at least the first two of them, are visible to a camera that's positioned at a vector3 of (0, 0, -10).

    Edit: When I plugged your code into my editor it threw some errors at me because you hadn't defined the array size. Based on lines 14 to 18 I assumed the array size as 4x4. With that in mind, the loop on line 20 needed to go to 4, not 5, since 0 counts as one of the loop iterations. Here it is as I formatted it:

    Code (CSharp):
    1. public class gpt : MonoBehaviour
    2. {
    3.     float length = 4;
    4.     float space = 3f;
    5.     int numGameObjects = 4;
    6.     public GameObject[] objectsToSpace;
    7.     Vector3[] positions = new Vector3[4];
    8.     int [,] grid = new int[4,4];
    9.  
    10.     void Start()
    11.     {
    12.       int length = 0;
    13.  
    14.       grid[1, 2] = 1;
    15.       grid[2, 1] = 1;
    16.       grid[2, 2] = 1;
    17.       grid[2, 3] = 1;
    18.       grid[3, 2] = 1;
    19.  
    20.       for (int i = 0; i < 4; i++)
    21.           if (grid[0, i] == 1)
    22.           {
    23.               length++;
    24.               Debug.Log(length);
    25.           }
    26.     }
    27.  
    28.     private void Update()
    29.     {
    30.         positions[0] = new Vector3(0, 0, 0);
    31.  
    32.         for (int i = 0; i < numGameObjects - 1; i++)
    33.         {
    34.             positions[i + 1] = positions[i] + new Vector3(length + space, 0, 0);
    35.         }
    36.  
    37.         for (int i = 0; i < numGameObjects; i++)
    38.         {
    39.             objectsToSpace[i].transform.position = positions[i];
    40.         }
    41.     }
    42. }
     
  13. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    226
    Thanks for your code, but it doesn't work.
    Indeed, no cubes, no spheres, no cylinders are displayed in the scene.
    Here are the different camera parameters that I tested:


    I have played with the array position[0] = new Vector3(X, Y, Z) and change the values X, Y, Z. But nothing appears.
    If you have examples that work?

    Thanks for your help,

    A+
     
    Last edited: Mar 15, 2023
  14. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    I think the issue with your two camera examples is that the first one has a y position of 20 and an x rotation of 60, so it would never see things at origin, and the second one is at origin, so it would also never see things at origin.

    Here's a unity 2019 example that works. And as I said before, you'll only see two of the objects because the other two are out of the cameras range to the right on the x axis.
    https://filebin.net/nhw0wh0x3l3s5p0i/Object_Spacing.zip
     
  15. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    226
    Thank you for your help,

    but when I change the elements in the ObjectsToSpace submenu of the inspector tab, the GameObjects don't change (see image below).

    More over I can't space my GameObjects with a small difference.



    And when I scale the cube on its width the cube is not spaced regularly (see below)


    Thanks again,

    A+
     
    Last edited: Mar 16, 2023
  16. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    You'll have to create your own algorithm to get them to space evenly when they're scaled different. I suggest doing that based on either the object's scale or its collider bounds to determine how wide it is.

    Go through the debugging process to figure out why the objects aren't changing when you replace them in the ObjectsToSpace variable. Stick lots of print or Debug.Log's in your script to help you figure it out.