Lee
Lee

Reputation: 43

Unity Listing below and moving if disabled

so here is the idea i have a couple of game objects in a UI listed below each other so the idea is as follows

GameObject1
GameObject2
GameObject3
GameObject4
GameObject5

so i want to disable GameObject4 but when doing so GameObject5 will stay in the position its at i want it to automatically move up in to GameObject4's position like so GameObject1
GameObject2
GameObject3
GameObject5

and not GameObject1
GameObject2
GameObject3

GameObject5

anyone have an Idea on how to do this?

Upvotes: 0

Views: 92

Answers (2)

Daveloper
Daveloper

Reputation: 758

Your question is pretty unclear and incomplete; even the (missing) line-breaks are confusing...

Since it's a UI and your question contains spaces and not-spaces, I guess your gameObjects are in a VerticalLayoutGroup? Far fetched, if they are, shame on you for not stating that in your question.

If you want the elements to be disabled without the lower elements to "move up"

If I understood your question correctly, the solution would be to put your content as a child of an other one like so:

enter image description here

EDIT: as @comphonia correctly suggests, it is preferable to have this menu made programmatically, with every button being a child of it's never disabled parent game object, and putting them into a List or array, such that you can disable them or reenable them individually

Upvotes: 0

comphonia
comphonia

Reputation: 521

Try creating an array of those objects (I think you mean UI elements) in an array, store their initial positions onStart in another array then loop to pop them from a stack.

Try fiddling with this to suit your needs:

 public GameObject[] buttons;
 float[] buttonPos;

private void Start()
{
    buttonPos = new float[buttons.Length];
    for (int i = 0; i < buttons.Length; i++)
    {
        buttonPos[i] =  buttons[i].transform.position.y;
        print(buttonPos[i]);
    }

}

private void Update()
{
    if (Input.GetKeyDown("space"))
    {
        DestroyButton(1);
    }
}

void DestroyButton(int i)
{
    Destroy(buttons[i]);
    Stack(i);
}

void Stack(int i)
{
    for ( int j= i; j < buttons.Length; j++)
    {
        if(j != buttons.Length-1)
        buttons[j + 1].transform.position = new Vector3(buttons[j + 1].transform.position.x, buttonPos[j], buttons[j + 1].transform.position.z);
    }

}

Upvotes: 1

Related Questions