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 Lock Elements of an Array

Discussion in 'Scripting' started by Hannibal11, Jan 19, 2021.

  1. Hannibal11

    Hannibal11

    Joined:
    Mar 5, 2020
    Posts:
    15
    Hello everyone, I have a problem. I need to spawn a formation of cubes, and so far so good ... but once the formation is spawned, I want to control all the cubes together, and not individually.
    Anyone know how to create a closed formation?
    Thanks!

    ps: in the photo the Script and the photo of the spawned cubes.
     

    Attached Files:

  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    what do you mean by "control all the cubes together"?

    Control as in "move around"? Stick them all in a parent GameObject (set transform.parent of the cubes to the transform of this parent GameObject) and then move that around. The cubes will inherit the parent transform.

    Control as in "alter the shader/material"? Then assign them all the same shared material. If you modify the shared material, all things the material is attached to will be updated.

    Control as in something else?
     
    Hannibal11 and Joe-Censored like this.
  3. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,495
    You could change InstantiateFormation function to do this:
    Code (CSharp):
    1. GameObject clone = Instantiate(legionary[l], positionToSpawn, rotationToSpawn, formationParent);
    And just add
    public Transform formationParent;
    and drag the parent you want to that, or create new GameObject in Awake or Start and assign to that.

    Edit: removed unnecessary whitespace and bracket from code block. Also, to clarify the second option, you could add this to Start before your call to CreateFormation:
    Code (CSharp):
    1. GameObject formationParentGO = new GameObject();
    2. formationParent = formationParentGO.transform;
    3. formationParent.SetParent(transform); // makes the formation a child of the calling transform
    4. formationParent.SetPositionAndRotation(Vector3.zero, Quaternion.Identity); // position your formation however
    Also you might want to consider using .localPosition and .localRotation with the children if you're moving and/or rotating the formationParent.

    Edit: fixed mistake (said 'formation' instead of 'formationParent')
     
    Last edited: Jan 19, 2021
    Hannibal11 likes this.
  4. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    You can either loop through the gameobjects applying the movement desired or use one as the parent of the rest and changes applied to the parent will apply equally to the children.
     
    Hannibal11 likes this.
  5. Hannibal11

    Hannibal11

    Joined:
    Mar 5, 2020
    Posts:
    15
    Thanks so much for your help,
    I tried to integrate your suggestions in the script, but in the Inspector if I insert the prefab of the cube it gives me an error: "Cannot instantiate objects with a parent which is persistent. New object will be created without a parent.".
    I want the first element of the formation to be the parent.

    I will add a flocking system to this formation. Will the flocking system work with child elements? or will they always assume the behavior of the parent? Thank you very much for helping.
     

    Attached Files:

  6. Hannibal11

    Hannibal11

    Joined:
    Mar 5, 2020
    Posts:
    15
    Yes: "Control as in move around"
    How do i put the "Transform" of a prefab in "Formation Paret"? If i do gives me this error:
    "Cannot instantiate objects with a parent which is persistent. New object will be created without a parent."
    If instead I create a cube in the Hirarchy it works.
    But I want the parent to be the first element of formation.

    Thanks for your help.
     

    Attached Files:

  7. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    Hannibal11 likes this.