Scaling parent without scaling children

In my game, I need to keep a cube to it’s scale, and it’s children to their own scale, but when I create children in it, it makes the children change their scale to the cube’s scale not their own. E.g. Cube’s scale: 1, 2, 1 Arrows’ Scale: 1, 1, 1. (The arrow, when playing, is the same scale as the cube.) And, the arrow changes between different cubes, depending on which one you clicked on.

What is the code you’re using to parent the arrow?

It’s going to look ugly, but here:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UnityEngine.UI;

public class MoveAxisScript : MonoBehaviour {

    public string Axis = "null";
    public string ObjName;
    public string PrevObjName;
    private bool ParentChanged;

    public float[] ParentScales;

    private void Start()
    {
        ParentChanged = false;
        PrevObjName = "Null";
        HideArrows();
    }

    public void HideArrows()
    {
        transform.position = new Vector3(999999999999, 999999999999, 999999999999);
    }

    void Run ()
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit))
        {
            if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject() != true)
            {
                ObjName = hit.transform.name;

                if (ObjName.Contains("OBJ") == true || ObjName.Contains("LIG") == true || ObjName.Contains("Player") == true)
                {
                    transform.position = hit.transform.position;
                    transform.SetParent(hit.transform);
                    GameObject.Find(ObjName).GetComponent<MeshCollider>().enabled = false;
                }

                if (PrevObjName != ObjName)
                {
                    if (ObjName.Contains("OBJ") == true || ObjName.Contains("LIG") == true || ObjName.Contains("Player") == true)
                    {
                        if (PrevObjName != "Null")
                        {
                            GameObject.Find(PrevObjName).GetComponent<MeshCollider>().enabled = true;
                        }

                        PrevObjName = ObjName;
                    }
                }


                if (hit.transform.name == "XAxisArrow")
                {
                    if (Axis == "null")
                    {
                        Axis = "X";
                    }
                    else if (Axis == "X")
                    {
                        Axis = "null";
                    }
                    else
                    {
                        Axis = "X";
                    }
                }
                else if (hit.transform.name == "YAxisArrow")
                {
                    if (Axis == "null")
                    {
                        Axis = "Y";
                    }
                    else if (Axis == "Y")
                    {
                        Axis = "null";
                    }
                    else
                    {
                        Axis = "Y";
                    }
                }
                else if (hit.transform.name == "ZAxisArrow")
                {
                    if (Axis == "null")
                    {
                        Axis = "Z";
                    }
                    else if (Axis == "Z")
                    {
                        Axis = "null";
                    }
                    else
                    {
                        Axis = "Z";
                    }
                }

                ParentChanged = true;
                if (ObjName.Contains("OBJ") == true || ObjName.Contains("LIG") == true || ObjName.Contains("Player") == true)
                {
                    GameObject.Find("XAxisArrow").GetComponent<AxisArrowScript>().ParentName = ObjName;
                    GameObject.Find("XAxisArrow").GetComponent<AxisArrowScript>().ChangeScale();

                    GameObject.Find("YAxisArrow").GetComponent<AxisArrowScript>().ParentName = ObjName;
                    GameObject.Find("YAxisArrow").GetComponent<AxisArrowScript>().ChangeScale();

                    GameObject.Find("ZAxisArrow").GetComponent<AxisArrowScript>().ParentName = ObjName;
                    GameObject.Find("ZAxisArrow").GetComponent<AxisArrowScript>().ChangeScale();
                }

               

            }
        }
    }

    void FixedUpdate()
    {

        if (Input.GetKey(KeyCode.Tab))
        {
            Axis = "null";
            ParentChanged = false;
            if (ObjName.Contains("OBJ") == true || ObjName.Contains("LIG") == true || ObjName.Contains("Player") == true)
            {
                GameObject.Find(ObjName).GetComponent<MeshCollider>().enabled = true;
            }

            if (PrevObjName.Contains("OBJ") == true || PrevObjName.Contains("LIG") == true || PrevObjName.Contains("Player") == true)
            {
                GameObject.Find(PrevObjName).GetComponent<MeshCollider>().enabled = true;
            }

            HideArrows();
        }

        if (Input.GetMouseButtonDown(0))
        {
            ParentChanged = false;
            Invoke("Run", 0.05f);
        }

        if (ParentChanged == true)
        {
            if (Axis == "X")
            {
                GameObject.Find("Position-X").GetComponent<InputField>().text = (transform.parent.GetComponent<SaveandLoad>().Position[0] += Input.GetAxis("Mouse ScrollWheel")).ToString();
            }
            else if (Axis == "Y")
            {
                GameObject.Find("Position-Y").GetComponent<InputField>().text = (transform.parent.GetComponent<SaveandLoad>().Position[1] += Input.GetAxis("Mouse ScrollWheel")).ToString();
            }
            else if (Axis == "Z")
            {
                GameObject.Find("Position-Z").GetComponent<InputField>().text = (transform.parent.GetComponent<SaveandLoad>().Position[2] += Input.GetAxis("Mouse ScrollWheel")).ToString();
            }

            ParentScales[0] = transform.parent.localScale.x;
            ParentScales[1] = transform.parent.localScale.y;
            ParentScales[2] = transform.parent.localScale.z;

            transform.eulerAngles = new Vector3(0, 0, 0);

        }

    }
}

Okay, well here’s what I can tell you about setting the parent. With 1 parameter, the world position stays (position, rotation, scale) true. If you pass a second argument as ‘false’, then it’ll be the scale of the parent.

So, you already use the method that should keep it the same. Perhaps I can’t spot it in your code, but do you change the scale of the cube while playing?

Yes

Okay, well you could work it out mathematically… you could do this, also:

Childcube.parent = null;
parentCube.localScale *= 2f;
Childcube.SetParent(parentCube);

OK, I did something similar:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AxisArrowScript : MonoBehaviour {

    public string ParentName;

    public void ChangeScale()
    {
        transform.parent = null;
        transform.position = GameObject.Find("MoveAxis'").transform.position;

        if (gameObject.name.Contains("X"))
        {
            transform.eulerAngles = new Vector3(0, 0, -90);
            transform.localScale = new Vector3(GameObject.Find(ParentName).transform.position.x / 2, GameObject.Find(ParentName).transform.position.x / 2, GameObject.Find(ParentName).transform.position.x / 2);
        } else if (gameObject.name.Contains("Y"))
        {
            transform.eulerAngles = new Vector3(0, 0, 0);
            transform.localScale = new Vector3(GameObject.Find(ParentName).transform.position.y / 2, GameObject.Find(ParentName).transform.position.y / 2, GameObject.Find(ParentName).transform.position.y / 2);
        } else if(gameObject.name.Contains("Z"))
        {
            transform.eulerAngles = new Vector3(90, 0, 0);
            transform.localScale = new Vector3(GameObject.Find(ParentName).transform.position.z / 2, GameObject.Find(ParentName).transform.position.z / 2, GameObject.Find(ParentName).transform.position.z / 2);
        }

        Invoke("ResetParent", 0.1f);
    }

    void ResetParent()
    {
        transform.SetParent(GameObject.Find("MoveAxis'").transform);
    }

}

However, when I run the ChangeScale void, this happens:
3420487--269853--upload_2018-3-11_19-44-38.png
And the object I have selected is 1x1x1

DERP! Never mind, I just had to change transform.position to transform.localScale