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. Dismiss Notice

Question change the initial position of a series of GameObjects

Discussion in 'Scripting' started by julien83franceschi, Jun 11, 2023.

  1. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    178
    Here's my code (it spaces and aligns several GameObjects with a regular space).

    Code (CSharp):
    1. public class SpacingObjects : MonoBehaviour
    2. {
    3.     public GameObject spherePrefab;
    4.     public GameObject capsulePrefab;
    5.     public GameObject cubePrefab;
    6.     float space = 2f;
    7.     private void Start()
    8.     {
    9.         float position = 0.5f;
    10.  
    11.         position = InstantiateAndPosition(spherePrefab, position);
    12.         position += GetPrefabWidth(spherePrefab) + space;
    13.  
    14.         position = InstantiateAndPosition(capsulePrefab, position);
    15.         position += GetPrefabWidth(capsulePrefab) + space;
    16.  
    17.         position = InstantiateAndPosition(cubePrefab, position);
    18.     }
    19.  
    20.     private float InstantiateAndPosition(GameObject prefab, float positionX)
    21.     {
    22.         Renderer prefabRenderer = prefab.GetComponentInChildren<Renderer>();
    23.         if (prefabRenderer != null)
    24.         {
    25.             float width = prefabRenderer.bounds.size.x;
    26.             Vector3 position = new Vector3(positionX + width / 2f, 0f, 0f);
    27.             Instantiate(prefab, position, Quaternion.identity);
    28.             return positionX + width + space;
    29.         }
    30.         else
    31.         {
    32.             Debug.LogWarning("Le préfabriqué ne contient pas de composant Renderer.");
    33.             return positionX;
    34.         }
    35.     }
    36.  
    37.     private float GetPrefabWidth(GameObject prefab)
    38.     {
    39.         Renderer prefabRenderer = prefab.GetComponentInChildren<Renderer>();
    40.         if (prefabRenderer != null)
    41.         {
    42.             return prefabRenderer.bounds.size.x;
    43.         }
    44.         else
    45.         {
    46.             Debug.LogWarning("Le préfabriqué ne contient pas de composant Renderer.");
    47.             return 0f;
    48.         }
    49.     }
    50. }
    I'd like to shift the series of GameObjects (cubes, capsules and spheres) to stage left.

    The problem is that the GameObjects are displayed from the center of the scene.

    If you can help me shift the GameObjects series to the left (by tweaking a few variables).

    I can't find the variable that sets the start of the alignment.

    I've tried the following variable:
    Code (CSharp):
    1. float position = -1f;
    but it doesn't change anything.

    Your help is most welcome.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Before you use the position in the Instantiate() call, change it.

    Code (csharp):
    1. position += new Vector3( offsetAmountInX, offsetAmountInY, offsetAmountInZ);
     
  3. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    178
    Hi,

    Sorry but position is a float.

    An I cant use the following code:

    Code (CSharp):
    1. position += new Vector3(-2, 0, 0);
    If you have any code,

    You are welcome.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Re-read my post. This is what I wrote:

    There is only ONE place in your code that does this: line 26 and 27.

    In that case,
    position
    is a
    Vector3
    .

    As for lines 11 to 17, don't use the word
    position
    . That is VERY confusing.
     
  5. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    178
    What code and variables do I need to modify to move my series of GameObjects to the left?

    Do I need to add or subtract the following code:

    Code (CSharp):
    1. new Vector3(-2, 0, 0)
    Your help is most welcome,

    A+
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    JUST TRY IT! That will be faster than posting!
     
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,084
    We're here to assist not hand you code but in order to do that you need to be able to at least understand what people are telling you. I feel like you've been handed code by someone and you don't even understand what it's doing. Which us handing you code isn't going to magically fix that. It's just going to shift the problem elsewhere.
     
    Kurt-Dekker likes this.