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

Prefab instantiating BEHIND background.

Discussion in 'UGUI & TextMesh Pro' started by okkal11, Sep 23, 2014.

Thread Status:
Not open for further replies.
  1. okkal11

    okkal11

    Joined:
    Jul 15, 2013
    Posts:
    16
    Hello

    I made a canvas -> panel -> image- That I want to be my background in the canvas.

    I then instantiate my 3D object prefab through script.



    When I run, i dont see the prefab, but all the UI looks fine. (EDIT: I can press the prefab through the background(it plays a sound))

    When I disable the background sprite (EDIT: or turn down background alpha) and run, I see the Prefab.

    So obviously I am instantiating the object behind the canvas itself-


    Moving the prefab closer to camera does nothing tho.

    How can i make the prefab render in front of canvas?

    Help appriciated.

    kal.
     
    Last edited: Sep 23, 2014
  2. TimBur

    TimBur

    Joined:
    Jan 17, 2013
    Posts:
    35
    Kal,

    There are several ways of controlling the relative position of the scene objects and the GUI. Without knowing your current settings, I can't give you a certain solution, but I can make an educated guess.

    I bet that your problem comes from the 'Render Mode' property of your Canvas. When you create a new Canvas, it starts out with 'Render Mode' = 'Screen Space - Overlay'. This means the GUI is drawn last, on top of everything else. No matter where you put your prefab, the GUI Canvas will always be drawn on top of the prefab, because you've told Unity to draw the Canvas as an overlay.

    If you want to have more control over what renders where, you need to change the Render Mode on your canvas. I suggest doing this:
    1) Change the Render Mode in your Canvas to 'Screen Space - Camera'.
    2) Grab your Main Camera object from the scene Heirarchy, and drag it into the Render Camera space in the Canvas.
    3) See the 'Plane Distance' option in the Canvas? This will now let you pick the distance between the camera and the plane. You can play with it. Figure out the distance between the camera and your prefab, then set the Plane Distance in the Canvas to some number bigger than that. Your prefab will now be drawn on top of your GUI.

    Lastly, it sounds like you're new to Unity. That's cool, and I'm glad you've come here, but I also think that if you were to work through some of the scripting tutorials, it would save you a lot of frustration and effort. I also suggest that you watch the 4.6 GUI tutorials here:
    http://unity3d.com/learn/tutorials/modules/beginner/ui
    The first tutorial covers Canvases, and explains about the different Render Modes.
     
  3. okkal11

    okkal11

    Joined:
    Jul 15, 2013
    Posts:
    16
    TimBur: That worked perfectly, and I am sure it will help alot of people out there.

    I will read up on this i promise!

    Thanks for your help <3
     
  4. TimBur

    TimBur

    Joined:
    Jan 17, 2013
    Posts:
    35
  5. valhalla_cats

    valhalla_cats

    Joined:
    Jan 27, 2013
    Posts:
    16
    This message could hardly be clearer. Thank you very much this has saved me lots of time.
     
  6. PhuocV

    PhuocV

    Joined:
    Dec 11, 2022
    Posts:
    1
    Ty so much my man.
     
  7. creseti

    creseti

    Joined:
    Aug 29, 2023
    Posts:
    1
    Thanks to you, it worked out well.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,417
    Please use the like button rather than necroing threads. This one is 9 years old.
     
Thread Status:
Not open for further replies.