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

Switching Z Depths at runtime

Discussion in '2D' started by Nik43, May 9, 2014.

  1. Nik43

    Nik43

    Joined:
    Dec 5, 2013
    Posts:
    6
    I've been searching tinkering away but still am unable to influence a game object's z-depth at runtime.

    I want to be able to have an object in front of another but appear to move behind the object, like hiding for example.

    I have the two objects on separate layers but on the same sorting layer with different z-depth values.

    I've been looking at manipulating the game object's Sprite Renderers but so far nuthin...:eek:

    Can any of you more experienced geezers point me in the right direction please?
     
  2. SiegfriedCroes

    SiegfriedCroes

    Joined:
    Oct 19, 2013
    Posts:
    569
    I don't really understand what you're trying to say here...
     
  3. Nik43

    Nik43

    Joined:
    Dec 5, 2013
    Posts:
    6
    In code I'm switching a game object's SpriteRenderer.sortingOrder SpriteRenderer.sortingLayerName in the attempt to make one game object start off in front of another game object but then appear behind that same other game object if say, i click a button.
     
  4. cbothra

    cbothra

    Joined:
    Mar 14, 2014
    Posts:
    125
    Use Transform.position.z or Transform.position
     
  5. Nik43

    Nik43

    Joined:
    Dec 5, 2013
    Posts:
    6
    Thanks, cbothra. Just realized that i was manipulating the control button not the actual object! (stupid).
    I'm getting the values to change but the visual result isn't happening, one object is not going behind another...
    I'll keep hacking, thanks again :)
     
  6. Nik43

    Nik43

    Joined:
    Dec 5, 2013
    Posts:
    6
    Got it working by manipulating the SpriteRenderer.sortingOrder. :p