I want to instantiate 2 prefab at a fixed interval at 5 min and 10 min.
But unable to find a proper method right now. I am doing like this.
int timer =(int) (Time.time % 5);
if (timer ==0)
{
//Instantiate first vaiable
Instantiate(KilometerPrefab, KilometerPos.position, Quaternion.identity);
}
int timer =(int) (Time.time % 10);
if (timer ==0)
{
//Instantiate Second vaiable
Instantiate(SecPrefab, Sec.position, Quaternion.identity);
}
I have google but unable to find a proper solution . Is there any better way?
Thank you so much for the reply.
Answer by Vicarian · Sep 05, 2018 at 01:33 PM
void Start() {
// second arg 0 if you want it to instantiate on Start()
InvokeRepeating("CreateKilometerObject", 300, 300);
InvokeRepeating("CreateSecObject", 600, 600);
}
private void CreateKilometerObject()
{
Instantiate(KilometerPrefab, KilometerPos.position, Quaternion.identity);
}
private void CreateSecObject()
{
Instantiate(SecPrefab, Sec.position, Quaternion.identity);
}
Hey Vicarian Thank you for answering. I wrote invokeRepeating funtion, This method instantiated after fixed time but after then it keep on istantiating it. I don't know why.
Thank you for answering. I am attaching the image.
[2]: /storage/temp/124059-error1.png
When you say you want to do something at a fixed interval it means that it should repeat. Now, if there's some condition by which you want to stop the spawns, you can build that in and use a boolean variable to return out from the method before instantiating. Or have I misunderstood you entirely?
Your answer
Follow this Question
Related Questions
Display a simple mesh (2D square) on the screen? 1 Answer
Masking based on angles 0 Answers
Make a 3D Object look like hes facing a point in an 2D space 0 Answers
Raycast is not hitting collider 2 Answers
Smooth touch movement on axis only while going up? 0 Answers