Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Parenting two objects

Discussion in 'Scripting' started by Feretro, Oct 20, 2022.

  1. Feretro

    Feretro

    Joined:
    Apr 18, 2017
    Posts:
    57
    Hello!
    I am having a problem that I can't understand.

    I have two objects, let's say OBJ_1 and OBJ_2

    I'd want that OBJ_1 detects the collision with OBJ_2 and makes it his Child.

    This code in on OBJ_1

    Code (CSharp):
    1.     private void OnTriggerEnter(Collider other)
    2.     {
    3.         transform.parent = other.transform.parent;
    4.     }
    watching in the inspector, this code won't work ...
    how may I acheive this?

    Thanks!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Code (CSharp):
    1. transform.parent = other.transform;
     
    Feretro and Homicide like this.
  3. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    637
    Watch out for that implentation. It would appear to me you may very well start parenting anything and damn near everything to that object. But yea, as praetor pointed out. Gl.
     
    Feretro and PraetorBlue like this.
  4. Feretro

    Feretro

    Joined:
    Apr 18, 2017
    Posts:
    57
    Thank you all! :)
    Now It works ... well now I have another problem (OBJ_2 falling and impacting OBJ_2 seems to change its scale!) but I will try to sort it out before asking for help in another tread.
    Anyway I would check if "tag" is the right one to avoid parenting everything! :)

    Thanks again!
     
    PraetorBlue and Homicide like this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Make sure whatever you parent to is ONLY scaled to (1,1,1)

    Generally if you're making katamari, and your outside ball is scaled (5,5,5), DO NOT parent picked-up items to that ball but instead perhaps the balls parent Transform, which should always be scaled (1,1,1)