Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Changing parent of an instantiated entities or existing entities

Discussion in 'Project Tiny' started by Zoelovezle, Feb 11, 2019.

?

Is it possible ?

  1. Yes

    2 vote(s)
    66.7%
  2. No

    1 vote(s)
    33.3%
  1. Zoelovezle

    Zoelovezle

    Joined:
    Aug 7, 2015
    Posts:
    54
    I am trying to create a grid based game on canvas .
    A simple solution was instantiate multiple canvas groups containing one button each and then attach that to canvas camera .
    But i am trying to make it more elegant as i dont want to instantiate multiple canvases and rather just have singular canvas and have buttons as child of it .
    So far exploring , i found out that the TransformNode attached to every child as the reference of its parent entity . Removing it and adding again in runtime or modifying the reference of TransformNode throws error "The component already exists"

    So i removed the transformnode manually while building and in runtime
    Code (JavaScript):
    1. this.world.setOrAddComponentData(blockEntity , new ut.Core2D.TransformNode(canvasEntity))
    Its runs but shows blank and since i can't actually inspect hierarchy i cant know for sure if it actually setting the parent as canvas entity or not
     
  2. Zoelovezle

    Zoelovezle

    Joined:
    Aug 7, 2015
    Posts:
    54
    The problem was scaling . Resolved it by scale back to (1,1,1) after setting the parent .
     
  3. ronblockparty

    ronblockparty

    Joined:
    Oct 13, 2018
    Posts:
    6
    Hey @Zoelovezle, sounds like you solved the issue, but for future purposes, I recently found out that if you pause execution in the editor while the game is running in your browser, it populates the hierarchy with entity and component values so you can inspect and debug them.

    See here: https://forum.unity.com/threads/help-debugging.608503/

    Hope that helps!
     
    Zoelovezle likes this.
  4. ronblockparty

    ronblockparty

    Joined:
    Oct 13, 2018
    Posts:
    6
    By the way, I found that setting the TransformNodeComponent's parent property isn't actually causing entities to become children of the parent. My code looks like this:

    Code (CSharp):
    1. const transformNodeComponent = this.world.getComponentData(blockEntity, ut.Core2D.TransformNode);
    2. transformNodeComponent.parent = this.world.getEntityByName("Board");
    3. this.world.setComponentData(blockEntity, transformNodeComponent);
    Inspecting the hierarchy, blockEntity doesn't become a child of the Board entity. Still trying to find a fix for this.
     
    Zoelovezle likes this.
  5. Zoelovezle

    Zoelovezle

    Joined:
    Aug 7, 2015
    Posts:
    54

    Make sure "Board" entity also has the component transform node attached