• 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 etto-bubu · Jan 29, 2018 at 03:19 PM · spritessorting order

update selected sprite's sortingOrder in relation with other sprites in the scene

Hello, Im trying to increase / decrease the sortingOrder of a selected sprite among other sprites in the scene on a button click. The number of sprites in the scene is defined by the number of sprites the user is spawning. For each new spawned sprite , its sortingOrder is increased based on the maximum number of sortingOrder of the old sprites in the scene. this is how i change the sorting order of a new spawned object

 for (var ob = 0; ob<manager.GetComponent<Manager>().objects.Length+1;ob++){
       GetComponent<SpriteRenderer>().sortingOrder= manager.GetComponent<Manager>().max+1;
  }

 

and this is how I get the maximum sortingOrder from the active sprites.

 void Update(){
   for (var ob = 0; ob<objects.Length; ob++)
    {
       int x = objects[ob].GetComponent<SpriteRenderer>().sortingOrder;
       toptempMax.Add(x);
    }
   for(int d = 0; d<toptempMax.Count; d++) 
   {
     max = Mathf.Max(toptempMax[0],toptempMax[d]);
    } 
 }

My problem question is how do I make that every time the user select an object and presses the layer button , the sortingOrder will change its value to be on top or underneeth other sprites within the scene? I tried using the max value to change the sortingOrder but It will give at some point two identical values within the sprites on the scene which will mess things up. a graphic showing that conflict when two identical values are within the scene.

alt text

alt text

The last selected sprite will have the same sortingOrder therefore will look as it is on top when in reality is under, and that because that it was spawned first within the scene.

Any help is welcome ^_^

screen-shot-2018-01-29-at-155244.png (35.8 kB)
screen-shot-2018-01-29-at-155306.png (35.5 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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

76 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 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 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 change values of GameObjects components in a foreach loop 1 Answer

Sprite Shape spline point order within layer 0 Answers

Sorting order jumbles with more than 30 objects in a single sprite mask 0 Answers

Semi-Transparency, Sprites, and Sorting Groups [Unity 3D] 1 Answer

Is it possible to change the texture property of a material via Animation clip? 2 Answers


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