- Home /
Co Routine
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!
I just formatted your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window before posting. When you don't mark your code as code, the text gets html escaped and it gets really hard to reformat it.
I have formatted this one for you. Please check the code is the same as what you have.
To help with your problem, read and try using Invoke
http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.Invoke.html
http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.InvokeRepeating.html
for example, in your main script that Instantiates fruit, in the start function call InvokeRepeating to instantiate a fruit every 3 seconds. Then on your fruit have a separate script. On the fruit script, in the start function, Invoke with a delay of 10 seconds to a command that destroys the fruit.
@alucardj: Destroy has a timer, so there's no real reason to use Invoke for that, or a separate script.
var fruitClone = Instantiate(bluefruit);
Destroy(fruitClone, 10.0);
true, I was keeping it simple and just directing focus to Invoke (thought Fattie would be happy to see that too).
But absolutely, my bullets have Destroy(gameObject, 10.0); in the start function. You raise an interesting point stating Destroy can be called from the instantiate script using the variable the fruit is assigned to, thanks.
Answer by fafase · Dec 09, 2012 at 08:14 AM
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...
Answer by Eric5h5 · Dec 09, 2012 at 07:45 AM
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.