Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

Thực thi câu lệnh sau khoảng thời gian trong Unity3D

8 năm trước 3.725 lượt xem 3 bình luận

chào mọi người!

cho mình hỏi khi viết script c# trong unity, mình tạo ra GameObject bằng lệnh Instantiate(...), làm sao để sau 2 giây thì nó mới sinh ra GameObject ấy ạ?

mình cảm ơn.

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập
KevinNg7en đã bình luận 3 năm trước

akduy2295 đã bình luận 8 năm trước

Bạn có thể dùng StartCoroutine hoặc InvokeRepeating, tuy nhiên, Coroutine được khuyên dùng vì performance tốt hơn Invoke
 

public float spawnDelay = 2;

void Start()
{
   StartCoroutine(CreateEnemy());
}
IEnumerator CreateEnemy()
{
   Instantiate(enemy, position, rotation); //position và rotation tự set
   yield return new WaitForSeconds(spawnDelay);
   StartCoroutine(CreateEnemy());
}

 

K9 SuperAdmin, KquizAdmin, KquizAuthor đã bình luận 8 năm trước

Bạn có thể thử

public GameObject ball;
 public float spawnTime = 3f;
 // Use this for initialization
 void Start () {
     InvokeRepeating ("SpawnBall", spawnTime, spawnTime);
 }
 
 // Update is called once per frame
 void Update () {
 }    
 void SpawnBall()
 {
     var newBall = GameObject.Instantiate(ball);
 }

 

Câu hỏi mới nhất