Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to pick up different objects and place old one back to initial position?

Discussion in 'Scripting' started by zyonneo, Sep 16, 2020.

  1. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    Hi I am trying to pick objects and place old one to initial position.There are many boots,gloves and dress.So if I try boots and applies it[Hides the current gameObject and shows it on the body].Then goes to next boot to try so the old boot already applied should go to initial position.Similarly other gloves and dress.
    The problem occurs when I apply boots >> then apply gloves >> then goes to apply boots again the last applied would be gloves so gloves goes back to initial position.
    So I tried the below code and it is working but some times when I hide it,it causing some problems[two objects gets hidden not always so not sure what is causing the prob.]The code is given below

    Code (CSharp):
    1.  
    2.     public List<GameObject> dressList= new List<GameObject>();
    3.     public List<GameObject> bootList = new List<GameObject>();
    4.     public List<GameObject> gloveList = new List<GameObject>();
    5.  
    6.  
    7. public void HideGameObject()
    8.     {
    9.  
    10.         pickedObject = GetGameObject();
    11.         hand.DetachObject(pickedObject);
    12.  
    13.         pickedObject.SetActive(false);
    14.     }
    15.  
    16. public void CheckPicked(GameObject otherGameObject, List<GameObject> selectedCatogery)
    17.     {
    18.  
    19.         foreach (var item in selectedCatogery)
    20.         {
    21.             if (otherGameObject.gameObject == item)
    22.             {
    23.                //Hides the game Object which is colliding with the trigger
    24.                 HideGameObject();
    25.  
    26.             }
    27.             else
    28.             {
    29.                 //Takes all other GameObjects fom the list and place them back to their position
    30.                 item.GetComponent<BackToPosition>().BackToInitialPosition();
    31.            
    32.                 item.SetActive(true);
    33.             }
    34.  
    35.  
    36.         }
    37.     }
    38.  
    39.         if (other.gameObject.tag == "Helmets")
    40.         {
    41.             CheckPicked(other.gameObject, helmetList);
    42.  
    43.         }
    44.  
    45.         if (other.gameObject.tag == "Gloves")
    46.         {
    47.             CheckPicked(other.gameObject, gloveList);
    48.  
    49.         }
    50.  
    51. Public class BackToPosition : MonoBehaviour
    52. {
    53.  
    54.     private Vector3 initialPosition;
    55.     private Quaternion initialRotation;
    56.  
    57.     // Start is called before the first frame update
    58.     void Start()
    59.     {
    60.         initialPosition = transform.position;
    61.         initialRotation = transform.rotation;
    62.     }
    63.  
    64.     // Update is called once per frame
    65.     void Update()
    66.     {
    67.  
    68.     }
    69.  
    70.     public void BackToInitialPosition()
    71.     {
    72.  
    73.         transform.position = initialPosition;
    74.         transform.rotation = initialRotation;
    75.  
    76.  
    77.     }
    78. }
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    I would change the approach here.
    Instead of storing dresses, boots, and gloves in their own lists, create a dictionary.
    Code (CSharp):
    1. enum ClothType
    2. {
    3.     Dress,
    4.     Boots,
    5.     Gloves
    6. }
    7.  
    8. Dictionary<ClothType, GameObject> equpiedClothByType;
    Use this dictionary to store all the data you need. When you want to equip gloves, for example, you say something along the lines of
    equpiedClothByType[Gloves] = ...

    At that moment, you also have a reference to the currently equipped piece of clothing so you can go ahead and hide it

    Also, this can be further extended with 0 work. All you have to do is add another field to the enum and magically, your entire system supports it.