Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Inune · Dec 05, 2016 at 07:32 AM · 2dtextmeshsorting order

Unity2D - Changing sorting order on text mesh makes text disappear??

I'm instantiating pickup sprites with a child gameobject that has a text mesh attached to it every two seconds. I'm trying to make it such that the text appears above the sprite but NOT above the sprites that get drawn after it. So I'm setting the sorting order for the text as 1 above the sorting order of its sprite, then incrementing the layer order number by TWO every time (so the next sprite overlaps the text of the last).

The result is very weird! Firstly, the sorting order for the sprites increments fine, but the text that's attached to them does not have a sorting order one higher than the sprite's, but equal to it instead (see console for text sorting order and inspector for sprite sorting order for the last object created). alt text Secondly, only the first object which spawns has its text drawn above it (the cloud with number 4 above!). The rest of them do not have any visible text (I assume it is being drawn below the sprite.)

I can make the text for all objects appear over the sprites if I put the sorting order = 0 (default), but then all text is above all objects, which I do not want.

I have already checked http://answers.unity3d.com/questions/595634/3d-textmesh-not-being-drawn-properly-in-a-2d-game.html This helped me learn how to access .sortingOrder and change it, but is not giving me the result I need. I'd really appreciate any help here, I've been so stuck on this for so long!!

PS: This is the first time I've posted so please let me know if I'm unclear or have put this question in the wrong place :)

 void Start()
 {
     layerOrder = 1;
 }

 // Update is called once per frame
 void Update()
 {
     timeSinceSpawn += Time.deltaTime;

     //spawn new pickup every 2 seconds
     if (timeSinceSpawn > 2)
     {
         Rigidbody2D newPickup = Instantiate(pickup);

         //set draw order for pickup
         newPickup.GetComponent<SpriteRenderer>().sortingOrder = layerOrder; 
         //find text mesh component on child
         Transform gameObjChild = newPickup.GetComponentInChildren<Transform>(); 
         //find text mesh renderer
         Renderer childRenderer = gameObjChild.GetComponent<Renderer>();     
         //set draw order for text on pickup. Must be 1 above so it can be seen over the pickup.
         childRenderer.sortingOrder = layerOrder + 1;    

         Debug.Log("Text sorting order: ");
         Debug.Log(childRenderer.sortingOrder);

         layerOrder += 2;
         timeSinceSpawn = 0;
     }
 }


sortingorderproblem.png (207.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Inune · Dec 06, 2016 at 08:43 PM

I've found some kind of a workaround which has done the job for me, though it might not be the best fix. I realized that the reason the text mesh had the same sorting order as the sprite (despite my code setting it as one above the sprite) was because it was a child of the sprite, and apparently children and parents must have the same sorting order!

So I changed the text mesh from being a child of the sprite to being its own gameobject, and made a prefab from that which I instantiate separately from the sprite. Then changing its sorting order works perfectly and I get the text underneath the sprites that spawn after it, but above the sprites that spawn before it.

Here's the final code:

 void Start()
 {
     layerOrder = 1;
 }

 // Update is called once per frame
 void Update()
 {
     timeSinceSpawn += Time.deltaTime;

     //spawn new pickup every 2 seconds
     if (timeSinceSpawn > 2)
     {
         //Instantiate sprite and set sorting order as before
         Rigidbody2D newPickup = Instantiate(pickup);
         newPickup.GetComponent<SpriteRenderer>().sortingOrder = layerOrder; 

         //Instantiate text object and set sorting order as 1 above sprite
         GameObject newText = Instantiate(pickupText);
         Renderer textRenderer = newText.GetComponent<Renderer>();
         textRenderer.sortingOrder = layerOrder + 1;

         layerOrder += 2;
         timeSinceSpawn = 0;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can I find all Game Objects in a certain Sorting Layer and dynamically change their position in the Sorting Order? 0 Answers

How do I make an array from text objects and get them to instantiate randomly? 1 Answer

Place UI text on top of other elements in 2D 1 Answer

default sprite missing after removing and reimporting tmp 0 Answers

3D object in 2D game 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges