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 chariot · Sep 09, 2015 at 02:55 PM · renderinghierarchyordering

Hierarchy rendering order problem

I want to change drawing order of GameObjects by changing their sibling indexes. There is the script I use:

 public class SiblingTest : MonoBehaviour {
     GameObject back;
     GameObject item1;
     GameObject item2;
     
     GameObject initGameObject(string name, string res) {
         GameObject target = new GameObject();
         target.name = name;
         target.AddComponent<SpriteRenderer>();
         target.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>(res);
         return target;
     }
 
     void Start () {
         back = initGameObject("back", "backSprite");
         back.AddComponent<BoxCollider2D>();
         back.GetComponent<BoxCollider2D>().isTrigger = true;
         
         item1 = initGameObject("item1", "item1Sprite");
         item1.transform.SetParent(back.transform);
         
         item2 = initGameObject("item2", "item2Sprite");
         item2.transform.SetParent(back.transform);
     }
     
     void Update () {
         if (Input.GetMouseButtonDown(0))
         {
             int currIdx = item1.transform.GetSiblingIndex();
             item1.transform.SetSiblingIndex((currIdx + 1) % 2);
             // here need to add some refresh function
         }
     }
 }
 

I got predictable result - on every click item1 and item2 change relative positions as childs of back in Hierarchy window. On the contrary of naive assumption, nothing happens on game screen. How do I need to update hierarchy tree to sync current hierarchy view and objects drawing order on game screen? Only way I found is adding { back.SetActive(false); back.SetActive(true);} as refresh. It do work, but left some strange feeling it's wrong and inefficient.

Comment
Add comment · Show 3
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
avatar image chariot · Sep 10, 2015 at 06:03 AM 0
Share

Up, whats up people?

avatar image chariot · Sep 10, 2015 at 09:07 AM 0
Share

I guess community only helps with "hello world" apps.

avatar image chariot · Jun 02, 2016 at 01:47 PM 0
Share

Up question again. For some reasons, our solution for this problem has a bad perfomance at big map. We make social strategy with many objects, it would be nice, if anybody can give us better solution. Thank you

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Suddoha · Sep 10, 2015 at 09:22 AM

What you're trying to do works for the UI which renders the objects according to the order in hierarchy. For sprites, you have to change the sorting order in a sorting layer.

Take this as an example and you'll be fine:

 public GameObject sprite1;
 public GameObject sprite2;
 
 private SpriteRenderer sr1;
 private SpriteRenderer sr2;
 
 void Start()
 {
     sr1 = sprite1.GetComponent<SpriteRenderer>();
     sr2 = sprite2.GetComponent<SpriteRenderer>();
 }
 
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Space))
     {
         sr1.sortingOrder = (sr1.sortingOrder + 1) % 2;
         sr2.sortingOrder = (sr2.sortingOrder + 1) % 2;
     }
 }
Comment
Add comment · Show 1 · 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
avatar image chariot · Sep 10, 2015 at 03:05 PM 0
Share

For some reasons we cant use sorting order for sprites, cause we have over 2000 gameobjects that creates procedural in game. Thats why we use sibling indexes.

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

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How To Sort Prefabs In Hierarchy? 1 Answer

Making UI elements appear in front of other elements 1 Answer

How to change UI rendering order without changing hierarchy 2 Answers

Searching for next available empty text field || instantiating object always on top of the hierarchy 0 Answers

Strange Hierarchy Lag 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