Hi all,
I am having a problem with setting a gameobject at a certain position in the hierarchy.
What I am trying to do is that I am having 10 gameobjects that represent small popup windows in the UI. so to show such a window I activate for example gameobject 7.
Now I want to prevent the player from clicking anything else in the UI that is next to the window (so the window should be modal). I also want to grey out the background a bit.
My solution for that is to activate the window, then activate a screen-filling half transparent panel and move it in the hierarchy directly above the window game object so it appears behind the window in the scene.
I am trying to do that by getting the sibling index of the window and then use panel.transform.SetSiblingIndex() to move the panel in its place. However, let’s say the index of the window is 7 and I set the panel index also to 7, the panel sometimes is above the window and sometimes below. Which has to mean that if a sibling index already exists, Unity has to decide somehow if the new inserted gameobject gets above or below but it is not consistent. Am I not getting something how to use it correctly?
Thanks for any help!
panel.SetActive(true);
panel.transform.SetParent(window.transform.parent, true);
panel.transform.SetSiblingIndex(window.transform.GetSiblingIndex());
1 Like
Determinism is about an action that acts on the previous state and ends up in the same end state. I’m pretty sure that your initial state is not the same in your test cases. Specifically moving objects around in a hierarchy has 2 major cases you have to consider:
- Either the object you move is below the target index so it has to be moved upwards
- or the object you want to move is above the target index, so it has to be moved downwards.
Those are two different cases and the result would be different. That’s because if the object is above the target index, removing the object from the hierarchy would move all other elements down one spot. Imagine you have a list that looks like this:
// Case 1 Case 2
//----------------------------
// 0 X 0 A
// 1 A 1 B
// 2 B 2 C
// 3 C 3 D
// 4 D 4 E
// 5 E 5 F
// 6 F 6 X
// Move the "X" element onto element "C"
// result
// target "3" target "2"
// --------------------
// 0 A 0 A
// 1 B 1 B
// 2 C 2 X
// 3 X 3 C
// 4 D 4 D
// 5 E 5 E
// 6 F 6 F
As you can see, in both cases we want to move the element “X” to the index of element “C”. However in the first case X ends up behind C since the elements have moved up one spot. In the second case X ends up before C since the elements have not moved. In any case SetSiblingIndex should move the object so its new sibling index is the one you set. In case 1 the target index is “3” (the index of the element “C”), in case 2 the target index is “2”
3 Likes
Oh thank you, you are absolutely right. I was not considering that the panel also becomes part of the sibling indexes and is counted but then moved away so the index becomes “invalid”.
Thanks to your detailed(!) explanation I was able to fix the issue, I test for moving before or after the current index of the panel and subtract 1 or not depending on the case. Working great now!
1 Like