using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ZOrder : MonoBehaviour {

    private List<SpriteRenderer> _mySpriteRenderer = new List<SpriteRenderer>();
    
	void Awake () {
        _mySpriteRenderer.AddRange(GetComponentsInChildren<SpriteRenderer>(true));
        if (GetComponent<SpriteRenderer>() != null)
            _mySpriteRenderer.Add(GetComponent<SpriteRenderer>());
	}

	// Update is called once per frame
	void Update () {

        var zOrder = (int)(transform.position.y * -100.0f);

        foreach (var spriteRenderer in _mySpriteRenderer)
        {
            spriteRenderer.sortingOrder = zOrder;
        }
        //Debug.Log("Z-Order " + name + " " + zOrder);
	}



}
