3D text and 2D sprites - I can't get the text to be "in front" of the sprites

So you know how you can have different sorting layers for sprites, so that you can put one in front or another behind? Well for 3D text objects there’s no such thing, and so I don’t know how to get my 3D text to appear infront of my sprite background. It always goes behind it so I can’t see it. I tried changing the Z value, to get it infront of the sprite, but that still didn’t work.

Is it possible to do this?

Renderer.sortingLayerID and/or Renderer.sortingOrder.

–Eric

4 Likes

Hi, this is my problem right now. it seems that it cannot show the 3d text on the game. :frowning: :frowning: tried changing z value too,but didn;t work… Im still having problems with this.

Hi Peeps,

Take a look at this:

Maybe it can solve your issues.

You know, I already posted the answer…use renderer.sortingLayerID and/or renderer.sortingOrder. Simple and it works.

–Eric

1 Like

Yep! It worked! Thank you!

1 Like

Its not in the Docs?

Hey, can a kind chap help me get this to work?

I’ve currently got this script attached to my text, which I want to appear in front of my sprites:

var textRenderer : Renderer;

function Awake ()
{
textRenderer = gameObject.GetComponent(Renderer);
}

function Start ()
{
textRenderer.sortingLayerID = 99;
}

Doesn’t really work. Is Renderer.sortingLayerID read only or something?

Was looking at this post also: http://forum.unity3d.com/threads/213817-Setting-Renderer-sortingLayerID-serialized-property?p=1434598&viewfull=1#post1434598

It looks very complicated, so I’m hoping it’s as simple as this thread suggests.

Thanks very much!
Romano

If it was, the docs would say so, and the script would generate an error. I really doubt you have 100 sorting layers in your project, so try using the layers you actually have.

–Eric

That is the actual answer, I just figured it out. Thanks :slight_smile: The sortingLayerID has to be created in the inspector beforehand. No idea if there’s a way of creating them from code.

I wish there was…

–Eric

I just learned something you all haven’t thought of…

I just wasn’t satisfied by fixing the sorting via script, so I was messing around and figured that I could create a sorting layer called “Background” behind Default sorting layer ( where all 3D objects and texts are ) and put everything that I wanted to be behind 3D objects in it. And ( as you may figured ) created a “Foreground” sorting layer in front of Default so if I wanted to but some sprite in front of the 3D stuff and it worked perfectly!

you’re welcome :wink:

6 Likes

I agree this is the best method and works.

Test example:

  1. Place on scene a GameObject with SpriteRenderer component on it. Look at property “Order in layer” - you can set that to bring your sprite in front of another, incrementing the value will bring your object close to camera.
  2. Place on scene a 3DText GameObject. Look at property “Offset Z” - you can set it to bring your text close to cam.

IMPORTANT!!!
If you want the text to be on top of sprite the “Offset Z” value should be bigger than “Order in layer” but it need to be a negative value, because 3DText works in 3D space, and SpriteRenderer works in 2D space, you need to set “Offset Z” (at 3DText gameObject) with a negative value to bring it more close to camera.

EX: offsetZ = (orderLayer + 1) x -1

This works for me. I hope this make sense and my english is enough readable. :smile:

1 Like

using UnityEngine;
using System.Collections;

public class SortingLayerExposer : MonoBehaviour
{

public string SortingLayerName = “Default”;
public int SortingOrder = 0;

void Awake ()
{
gameObject.GetComponent ().sortingLayerName = SortingLayerName;
gameObject.GetComponent ().sortingOrder = SortingOrder;
}
}

this will be work.you are welcome !

1 Like

Perfect thanks, just slide the default Sorting Layer to the level you want the text to render.
2166848--143348--upload_2015-6-19_15-21-59.png

1 Like

The final touch that helped me with this issue after following the above two suggestions was to add a Canvas to my text label - the Canvas then allowed ‘override sorting’ which then let me set a sorting layer for it.

Epic for something that should be so simple.

1 Like

its not working for me , this is my code :
GameObject.Find(“frontP”).GetComponent().sortingLayerID = 99;

and unity gives me :
Invalid layer id. Please use the unique id of the layer (which is not the same as its index in the list).

It’s a defect?? with the TextMesh. They didn’t add the 2 values to the inspector. Create a Script to override it those values. You won’t see it at design time, but at runtime it works fine

Cant believe this Renderer.sortingLayerID and/or Renderer.sortingOrder is the solution for my sprite not appearing in my mesh background. Been stuck here for almost 3 day.