C# 计时器没有改变精灵

C# 计时器没有改变精灵,c#,visual-studio-2010,unity3d,C#,Visual Studio 2010,Unity3d,我目前正在开发一款具有精灵效果的游戏。玩家有能力在一定时间内改变他们的“冰刀”。2秒后,我希望效果恢复到正常的“冰刀”。因此,我需要一个统一的计时器。我试了很长时间,但还是没能成功 以下是我的示例代码: if (other.gameObject.CompareTag("IcePickup")) { Destroy(other.gameObject); timer -= Time.deltaTime; timer ++; Bl

我目前正在开发一款具有精灵效果的游戏。玩家有能力在一定时间内改变他们的“冰刀”。2秒后,我希望效果恢复到正常的“冰刀”。因此,我需要一个统一的计时器。我试了很长时间,但还是没能成功

以下是我的示例代码:

if     (other.gameObject.CompareTag("IcePickup"))
{
        Destroy(other.gameObject);
        timer -= Time.deltaTime;
        timer ++;
        Blade1 = GameObject.Find("Blade1");
        Blade1.gameObject.GetComponent<SpriteRenderer>().sprite = IceBlade;
        if (Blade1.gameObject.GetComponent<SpriteRenderer>().sprite = IceBlade)
    {
//i put a timer here but i cant figure out why is isnt working and print isnt showing.
        if (timer > 2)
        {
            Blade1.gameObject.GetComponent<SpriteRenderer>().sprite = BladeNormal;
            print("working");
        }
    }

}
if(other.gameObject.CompareTag(“IcePick”))
{
销毁(其他游戏对象);
timer-=Time.deltaTime;
定时器++;
Blade1=GameObject.Find(“Blade1”);
Blade1.gameObject.GetComponent().sprite=IceBlade;
如果(Blade1.gameObject.GetComponent().sprite=IceBlade)
{
//我在这里放了一个计时器,但我不明白为什么它不工作,打印也不显示。
如果(计时器>2)
{
Blade1.gameObject.GetComponent().sprite=BladeNormal;
打印(“工作”);
}
}
}

要检查它们是否相等,您应该这样写

  if (Blade1.gameObject.GetComponent<SpriteRenderer>().sprite == IceBlade)
仅当
other.gameObject.CompareTag(“ICEPeack”)
为true时发生一次 在这里

timer -= Time.deltaTime;
        timer ++;
您正在减小计时器值,同时增大它


所以这显然不是计时器。使计时器
timer-=Time.deltaTimeUpdate
中调用code>才能计数,您也可以使用
coroutines
,并且是两种方法的实现,以检查它们是否相等。您应该这样编写它

  if (Blade1.gameObject.GetComponent<SpriteRenderer>().sprite == IceBlade)
仅当
other.gameObject.CompareTag(“ICEPeack”)
为true时发生一次 在这里

timer -= Time.deltaTime;
        timer ++;
您正在减小计时器值,同时增大它

所以这显然不是计时器。使计时器
timer-=Time.deltaTimeUpdate
中调用code>才能计数,也可以使用
coroutines
,这两种方法都可以实现