- Home /
InvokeRepeating() only executes once? Also Update () doesnt repeat?
Okay so Q1)Why does InvokeRepeat ( "functionname",initialtime,repeatRate) calls my function only once? I have tried keeping it in start() but only got 1 object (my function instantiates a prefab) and even in Update() it only spawned once.The for loop in Start() does indeed spawn 10 food in different locations! public class FoodSpawner : MonoBehaviour { public GameObject food; float x, z,y = 1.0f; public float maxx, minx, maxz, minz; float timeBetweenSpawn = 5.0f; float nextSpawnTime; float InstantiationTimer = 5.0f;
void Start()
{
/*int i = 0;
for(i=0;i<10;i++)
{
spawnFood();
}
*/
}
void repeatFoodSpawn()
{
if (Time.time >= nextSpawnTime)
{
spawnFood();
nextSpawnTime += timeBetweenSpawn;
}
}
void randomFoodLocationProvider()
{
x = Random.Range(minx, maxx);
z = Random.Range(minz, maxz);
}
void spawnFood()
{
randomFoodLocationProvider();
Instantiate(food, new Vector3(x, y, z),Quaternion.identity);
}
// Update is called once per frame
void Update()
{
InvokeRepeating("spawnFood", 0, 1);
}
}
currently the InvokeRepeating is in Update() but I tried in Start() also ; I also tried those Time.time trick which gradually decreases time until its les than 0.1 to trigger but didnt work Q2)If i just keep SpawnFood() in Update() shouldnt it call it every frame? but NO it doesnt At this point it makes me think that there is something wrong in my food :P Thanks for any leads on this !
Your answer
Follow this Question
Related Questions
Problem instantiating a prefab, and storing a reference to it. 1 Answer
How to instantiate an object on multiple positions? 0 Answers
Intantiated objects being created but being invisible...[PICTURES] 1 Answer
why is it slow to instantiate 0 Answers
instantiating prefab does not rotate child objects correctly? 1 Answer