Search Unity

Canvas as child of a gameObject behaves strangely

Discussion in 'UGUI & TextMesh Pro' started by Ciryus, Sep 3, 2014.

  1. Ciryus

    Ciryus

    Joined:
    Sep 17, 2012
    Posts:
    38
    OKay let's say I have a player, for convenience I use a Canvas as a child of this player, player who is instantiated during game.

    the problem is that the canvas appears to be in the right poisition (using Screen Space - the camera of the player) but every child is at 0 position depending on his rect alignment, e.g. if aligned to center, every element will be in the center of my screen)

    Same idea but different behavior with worldSpace canvas which apperas to be at the 0 position of its parent when instantiated.

    I think I've read about using transform.SetParent() but don't really see how it works to replace my elements on the position I gave them in the prefab.

    Could you light me up on what I am missing?
    Thanks.
     
  2. Ciryus

    Ciryus

    Joined:
    Sep 17, 2012
    Posts:
    38
    Just to be a little more precise here is the configuration of my Instantiated Prefab:

    -MyPlayer
    ---MyCamera
    -------MyCanvas (Screen Space Camera)
    ----------MyUIElements positionned over the canvas (which becomes 0 when the MyPlayer is Instantiated)

    The reason why I put the canvas in my Prefab is that I need to access it from MyPlayer script, and the Render Camera attached to my UI is also in the prefab, which would break connection if all these were not in the same prefab.
     
  3. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,729
    Noticed something similar myself.

    I'm trying to have healthbars for my enemies. I want them to be screen space. But if I have a screen space canvas as a child of my enemy it changes position based on the position of the enemy so the healthbar moves all over the place. Obviously not what I expected. :)