Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.
Options

Are there rules for when I can "parent" a GameObject to another? .localLocation acts weird.

I am trying to understand when and how I should parent GameObjects.

Here's some code I have that tries to create a sub object and a child object to THAT sub object. For some reason, the object appears in a totally different location when I do this. Here's the code that DOESN'T work:

void Start () {
    var go = gameObject;
    // Add the move button.
    var buttonContainer = new GameObject("moveButtonContainer", typeof(InteractionButton));
    buttonContainer.transform.parent = go.transform;
    var cube = GameObject.Find("MyCube");
    var obj = GameObject.Instantiate(cube);
    obj.transform.parent = buttonContainer.transform; // This line will change below to make this work.
    obj.transform.localPosition = new Vector3(0, 0, 0);

However, if I parent the object I created to "go" (the current game object - see the first line), then this works as expected (the object is placed correctly):

void Start () {
    var go = gameObject;
    // Add the move button.
    var buttonContainer = new GameObject("moveButtonContainer", typeof(InteractionButton));
    buttonContainer.transform.parent = go.transform;
    var cube = GameObject.Find("MyCube");
    var obj = GameObject.Instantiate(cube);
    obj.transform.parent = go.transform; // This is the line that changed
    obj.transform.localPosition = new Vector3(0, 0, 0);

Why can't I create intermediate objects and parent to them?

Tagged:

Best Answer

Answers

  • Options

    Unity defaults to preserving world space for your object when reparenting. obj.transform.SetParent has an overload that will preserve local position.

    ===
    This post provided as-is with no warranties and confers no rights. Using information provided is done at own risk.

    (Daddy, what does 'now formatting drive C:' mean?)

Sign In or Register to comment.