Используем Invoke. Метод hlop, сработает через 2 секунды после запуска скрипта
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StownGo : MonoBehaviour
{
Rigidbody stown;
public GameObject StownFloor;
void Start()
{
stown = GetComponent<Rigidbody>();
stown.velocity = 30 * transform.forward;
Invoke("hlop", 2.0f);
}
void hlop()
{
Destroy(gameObject);
}
}
Использование InvokeRepeating. Данная команда вызовет метод «hlop» через 2 секунды, и будет вызывать его с интервалом в одну секунду:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StownGo : MonoBehaviour
{
float a = 0;
void Start()
{
InvokeRepeating("hlop", 2.0f, 1f);
}
void hlop()
{
a = a + 1;
if(a>10)
{
CancelInvoke(); //отключаем цикл при условии, что а>10
}
}
}
Следующий скрипт будет запускать звуковой файл audio один раз в секунду:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class time : MonoBehaviour
{
public float delay;
public float TimerDelay=1f;
AudioSource audio:
void Start()
{
delay = TimerDelay;
audio = GetComponent<AudioSource>();
}
void Update()
{
delay = delay - Time.deltaTime;
if(delay<=0)
{
audio.Play();
delay=1;
}
}
}
Пауза, вызов игрового меню, выход из игры:
using UnityEngine;
using System.Collections;
public class Rules : MonoBehaviour
{
public float timing;
public bool isPaused;
public GameObject menu;
void Start()
{
menu.SetActive(false);
}
void Update()
{
Time.timeScale = timing;
if (Input.GetKeyDown(KeyCode.Escape) && isPaused == false)
{
isPaused = true;
}
else if (Input.GetKeyDown(KeyCode.Escape) && isPaused == true)
{
isPaused = false;
}
if (isPaused == true)
{
timing = 0;
menu.SetActive(true);
}
else if (isPaused == false)
{
timing = 1;
menu.SetActive(false);
}
}
public void ResumeButton(bool state)
{
isPaused = state;
}
public void QuitButton()
{
Application.Quit();
}
}