Search Unity

Question Lock child to parent transform

Discussion in 'Editor & General Support' started by miccalisto, Mar 5, 2023.

  1. miccalisto

    miccalisto

    Joined:
    Feb 20, 2018
    Posts:
    40
    How do I lock a child to parent transform in the editor, so that I can't accidently move the child? If thats possible.
     
    Last edited: Mar 5, 2023
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    If your issue is that you have, say, a Player object with several child transforms, and you keep accidentally selecting and moving the child instead of the root, try adding the
    [SelectionBase]
    attribute to the Player (or equivalent) component on the root.

    This will make all clicks on children objects defer the selection to the SelectionBase object first by default. I would recommend this course of action first, as it is a cleaner solution and prevents accidents without reducing functionality.

    You can also try selection-locking the child in the Hierarchy view, if you don't need to be able to select the child at all.

    If you absolutely want to lock the child transform, you can use an editor script to override the transform position:

    Code (CSharp):
    1. [ExecuteAlways]
    2. class LockLocalPosition : MonoBehaviour {
    3. #if UNITY_EDITOR
    4.   void Update () {
    5.     transform.localPosition = Vector3.zero;
    6.   }
    7. #endif
    8. }
     
    Last edited: Mar 5, 2023