Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Triggerzz · Feb 24, 2020 at 10:52 PM · 2dgameobjecttransform3d

Return Object to Original Position after it's Disabled

I have a parent object (Extras Container) with multiple child objects: alt text

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.

extras.png (3.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Triggerzz · Feb 25, 2020 at 01:44 AM 0
Share

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!

avatar image unity_ek98vnTRplGj8Q Triggerzz · Feb 25, 2020 at 03:22 PM 1
Share

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;
         }
     }
 }
avatar image Triggerzz unity_ek98vnTRplGj8Q · Feb 26, 2020 at 09:51 PM 0
Share

Oh yep, the fault was one my part, thanks again!!! Works great!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

403 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges