Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Spawning Canvas and Child Buttons from prefab - Rect Transform info lost

Discussion in 'UGUI & TextMesh Pro' started by ultranol, Sep 14, 2014.

  1. ultranol

    ultranol

    Joined:
    Apr 1, 2014
    Posts:
    10
    Hello,

    I'm not sure whether this is a bug, or just user-unfriendly behaviour;

    I have my Canvas as a prefab, and my buttons as children. Everything in my test scene is fine, correct sizes, placement etc. However when I spawn it in a level, all settings seem to be fine - except for the Rect Transforms for my buttons. Everything is appearing at their anchor points - so in the corners and clipped offscreen.

    I have ReferenceResolution set, and as I said, everything is fine in my test scene.

    This shouldn't be happening, right?
     
  2. JAKJ

    JAKJ

    Joined:
    Aug 17, 2014
    Posts:
    185
    childObject.SetParent(parentObject,false)
     
  3. ultranol

    ultranol

    Joined:
    Apr 1, 2014
    Posts:
    10
    So after spawn I need to clear the Parent from my buttons?

    (I'm using Playmaker, so I just need to know which object - button or canvas that I'm setting that property for)
     
  4. ultranol

    ultranol

    Joined:
    Apr 1, 2014
    Posts:
    10
    The bit on the FAQ seemed to imply that buttons spawned then need to be set as children of the canvas. My object hierarchy on the prefab does that already - it's identical to how it is in my test scene, which works perfectly.
     
  5. Nanity

    Nanity

    Joined:
    Jul 6, 2012
    Posts:
    148
    I can confirm this behaviour. More specificly only the anchor information is lost. All prefabs are anchored to bottom left.

    RectTransform.anchorMin/Max() aren't helpfull either, why are there two Vector2() instead of a Vector4? Helpfull would be a preset of anchor functions for the 16 default variations, too. Or at least giving a better understanding how the normalized anchor system works in the manual.

    Edit: Found the issue. Instantiate() should be called with the prefab parameter ONLY, do not override the position and rotation.

    Nontheless I still vote for a Vector4(float top, float right, float bot, float left) anchor.
     
    Last edited: Sep 15, 2014
    glowe likes this.
  6. ultranol

    ultranol

    Joined:
    Apr 1, 2014
    Posts:
    10
    My prefab anchors of bottom-left, top-left, top-right were all maintained correctly, it was just the anchor values that was lost. (so all items appearing at their anchor positions)

    Also - @JAKJ , can you please confirm what you meant be your reply? It doesn't seem relevant, and it wasn't really helpful without any kind of context or other information.
     
  7. JAKJ

    JAKJ

    Joined:
    Aug 17, 2014
    Posts:
    185
    With this new UI, the #1 cause of "my stuff isn't where it should be" is not using SetParent with false, so since your original post was somewhat vague and unhelpful, that was the best guess available.
     
  8. ultranol

    ultranol

    Joined:
    Apr 1, 2014
    Posts:
    10
    In what way was the original post vague? It perfectly describes my issue and my setup.

    I specifically stated that my prefab was 'Canvas and then buttons as child objects'.

    In future, I would suggest you actually read the contents of a post before replying with an unhelpful and irrelevant reply. It doesn't help anyone.
     
  9. glowe

    glowe

    Joined:
    Jun 10, 2014
    Posts:
    68
    Nanity - thanks for this answer. All positions work fine when I follow your suggestion.

    Sure enough, my wonky canvas was instantiated with:
    Code (CSharp):
    1. Instantiate(prefab, location, Quaternion.identity);
    It worked as intended when I used:
    Code (CSharp):
    1. Instantiate(prefab);
    I wonder why instantiate with position and rotation messes some child positions...
     
    Last edited: Oct 1, 2014
    Nanity likes this.
  10. PeteD

    PeteD

    Joined:
    Jul 30, 2013
    Posts:
    71
    Cheers Nanity. Your reply to this helped me with an issue I was seeing in my code too.
     
    Nanity likes this.
  11. VideoJames

    VideoJames

    Joined:
    Aug 23, 2012
    Posts:
    10
    Thanks JakJ. You saved my day.