Return Object to Original Position after it's Disabled
I have a parent object (Extras Container) with multiple child objects:
Whenever I press a button, one of the objects is enabled. Only one object can be visible at the same time. Whenever an object is enabled, I can select it and move it around with my mouse.
What I want is whenever I switch between objects, the new object takes the place of the former object (which is currently how it works). But also, when there are NO objects enabled, and I enable an object again, I want it's position to return to it's original position where it started.
So, when I switch between "currentObject," the position stays the same between them. But when no objects are enabled, and I enable the object again through button press, the "currentObject" returns to its "originalPosition."
Thanks to @unity_ek98vnTRplGj8Q who helped me with this code, which is how things are set up:
public GameObject extrasContainer;
private GameObject currentObject;
private GameObject[] childrenObjects;
public Vector3 originalPos;
public void Start()
{
foreach (Transform t in extrasContainer.GetComponentsInChildren<Transform>(true))
{
if (t.gameObject.activeSelf && t.parent == extrasContainer.transform) currentObject = t.gameObject; //Save whichever one you have activated at the start
t.gameObject.SetActive(true);
}
childrenObjects = new GameObject[extrasContainer.transform.childCount];
foreach (Transform t in extrasContainer.GetComponentsInChildren<Transform>(true))
{
if (t.parent == extrasContainer.transform) childrenObjects[t.GetSiblingIndex()] = t.gameObject;
}
foreach (Transform t in extrasContainer.GetComponentsInChildren<Transform>(true))
{
if (t.gameObject != currentObject)
{
if (t.parent == extrasContainer.transform) t.gameObject.SetActive(false);
}
}
}
//HOOKED UP TO BUTTON
public void ChooseObject(int index)
{
if (index < extrasContainer.transform.childCount)
{
GameObject newObject = childrenObjects[index].gameObject;
if (newObject != currentObject)
{
if (currentObject != null) currentObject.SetActive(false);
newObject.SetActive(true);
currentObject = newObject;
currentText.text = currentObject.name;
}
else
{
currentObject.SetActive(false);
currentObject = null;
}
}
}
I tried using:
currentObject.transform.position = originalPos;
In the else statement before the currentObject is disabled, but it doesn't work correctly.
Answer by unity_ek98vnTRplGj8Q · Feb 24, 2020 at 11:15 PM
Instead of doing it on disable, try doing all the logic explicitly when you enable.
if (newObject != currentObject) {
newObject.SetActive(true);
if (currentObject != null) {
newObject.transform.position = currentObject.transform.position;
currentObject.SetActive (false);
}
else{
newObject.transform.position = originalPos;
}
currentObject = newObject;
currentText.text = currentObject.name;
}
Thanks again for helping! I tried out the code and it works well! The object does go back to the original position when I press the same button again after moving it. And it also switches through the objects when needed.
However, with this code, an object is always enabled. I still want to toggle an object on or off by pressing the same button.
For instance, if I press the button to enable Cube, I want to disable it by pressing the same button. If there are no objects enabled, the object will always go back to the originalPosition. But if Cube is already enabled and I press the button for Sphere, then Sphere takes over Cube's transform. It doesn't go back to the originalPosition (This part already works in the code though! The only thing that doesn't work is disabling the object when pressing the same button).
I'm sorry if I'm not explaining this clearly, let me know if you need a better explanation! X0 Thanks again!
Its should still work the same, make sure you didn't take out the part where it disables the object
public void ChooseObject (int index) {
if (index < extrasContainer.transform.childCount) {
GameObject newObject = childrenObjects[index].gameObject;
if (newObject != currentObject) { //Enable different object
newObject.SetActive (true);
if (currentObject != null) { //Put object in old object's place
newObject.transform.position = currentObject.transform.position;
currentObject.SetActive (false);
} else { //Reset position
newObject.transform.position = originalPos;
}
currentObject = newObject;
currentText.text = currentObject.name;
} else { //Disable active object
currentObject.SetActive (false);
currentObject = null;
}
}
}
Oh yep, the fault was one my part, thanks again!!! Works great!
Your answer
Follow this Question
Related Questions
Find active Child gameobject out of multiple gameobjects 2 Answers
Why does destroying a gameObject influence its former children? 1 Answer
[SOLVED]How do I spawn GameObject[] in Transform[]? 2 Answers
How To Make An Object Appear In Front Of Player Without Cloning? 1 Answer
Set One GameObject Inactive If Another GameObject Is Active? 1 Answer