Unity. Function call after a certain period of tim

2019-02-25 13:43发布

How can I make an object invisible (or just delete) after a certain period of time? Use NGUI.

My example (for changes):

public class scriptFlashingPressStart : MonoBehaviour  
{   
    public GameObject off_Logo;
    public float dead_logo = 1.5f;

    void OffLogo()  
    {       
        off_Logo.SetActive(false);  
    }

    //function onclick button
    //remove item after a certain time after pressing ???
    void press_start()
    {
        InvokeRepeating("OffLogo", dead_logo , ...);
    }
}

3条回答
闹够了就滚
2楼-- · 2019-02-25 14:01

Use Invoke rather than InvokeRepeating. check Invoke function here

 public class scriptFlashingPressStart : MonoBehaviour  
    {   
        public GameObject off_Logo;
        public float dead_logo = 1.5f;
        bool pressed = false;

    void OffLogo()  
    {       
       //do anything(delete or invisible)
        off_Logo.SetActive(false);
         pressed = false;  
    }

   //use Invoke rather than InvokeRepeating
    void press_start()
    {
        if(!pressed)
        {
          pressed = true;
          Invoke("OffLogo", dead_logo);
        }
        else
        {
          Debug.Log("Button already pressed");
        }
    }
}
查看更多
一纸荒年 Trace。
3楼-- · 2019-02-25 14:15

try

StartCoroutine(SomeFunctionAfterSomeTime);

IEnumerator SomeFunctionAfterSomeTime()
{
    ... //Your own logic
    yield return new WaitForSeconds(SomeTime);
}
查看更多
等我变得足够好
4楼-- · 2019-02-25 14:16

You can destroy an object in a given time by simply calling Destroy. See http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html

查看更多
登录 后发表回答