Hello community, I don’t see an obvious answer to my question on this site, so i am sending out. I have tried to create a co-routine to do the following:
–Instantiate a prefab of 'bluefruit" every 3 seconds…
–destroy the bluefruit after 10 seconds if they are not ‘claimed’ by the player `
–Here is the code. This script is assigned ot an Empty GameObject in the scene. The point is that the user will have 3 seconds to collect the blue fruit . Please advise.
pragma strict
var bluefruit : GameObject;
var numberOfFruits : int;
function Start ()
{
StartCoroutine(GenerateFruits());
numberOfFruits = 20;
}
function Update()
{
}
function GenerateFruits()
{
while (numberOfFruits > 0)
{
Instantiate(bluefruit);
yield new WaitForSeconds(3.0f);
}
}
at the moment, nothing is happening! Thank you for taking the time to respond to me!
Public variables take their value from the inspector. You start the coroutine, with numberOfFruits presumably set to 0 in the inspector, so the GenerateFruits function does nothing and exits. You then set numberOfFruits to 20 afterward in code, but at that point it has no effect since nothing is using that variable.
fafase
3
pragma strict
var bluefruit : GameObject;
var numberOfFruits : int=20;
function Start () {
InvokeRepeating("GenerateFruits",0.01f,3.0f);
}
function GenerateFruits() {
var fruit=Instantiate(bluefruit,position,rotation);
Destroy(fruit,10.0f)
if(--numberOfFruit <=0)CancelInvoke();
}
This is how I would try it. Am I totally right? You tell me…