排序顺序无法正常工作

时间:2016-04-14 19:56:56

标签: unity3d unity5

我正在尝试为2D游戏更改统一排序图层的顺序,但以下脚本对我不起作用:

using UnityEngine;
using System.Collections;

public class LevelManager : MonoBehaviour {

    public GameObject player;
    public SpriteRenderer deadGuy;

    // Use this for initialization
    void Start () {

    }

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

        if (player.transform.position.y < deadGuy.transform.position.y)
        {
            deadGuy.sortingOrder = 0;
        } else
        {
            deadGuy.sortingOrder = 2;
        }

    }
}

在运行游戏之前,对象已在检查器窗口中统一链接。

修改

这是我的代码:

using UnityEngine;
using System.Collections;

public class LevelManager : MonoBehaviour {

    public GameObject player;
    public GameObject deadGuy;
    public bool belowTheY;

    // Use this for initialization
    void Start () {

        deadGuy.GetComponent<SpriteRenderer>().sortingOrder = 2;

    }

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

        if (player.transform.localPosition.y < deadGuy.transform.localPosition.y)
        {
            belowTheY = true;
            deadGuy.GetComponent<SpriteRenderer>().sortingOrder = 0;

        } else
        {
            belowTheY = false;
            deadGuy.GetComponent<SpriteRenderer>().sortingOrder = 2;
        }

    }
}

如果用户低于deadGuy的Y位置,则会激活。所以我知道if语句正在正确执行。但是,排序层没有被更改

1 个答案:

答案 0 :(得分:0)

似乎玩家对象也在排序顺序0上,所以当deadGuy对象编写脚本以进入排序顺序0时,由于优先权,它仍然显示在前面。