Skip to main content
Added some explanation.
Source Link
John Hamilton
  • 3.1k
  • 1
  • 16
  • 34

Well, all those co-routines and waiting for some seconds are nice but, why not just use Invoke() for this purpose as well?

//...
public void SpawnObject (){
    var warningDelay = 5.0;
    Instantiate (spawnee, transform.position, transform.rotation);
    Invoke("CreateWarningImage", warningDelay);
    if (stopSpawning) {
        CancelInvoke ("SpawnObject");
    }

}
//someTransform is where you want the image to be, assuming you've already defined it
public void CreateWarningImage(){
    Instantiate (warning, someTransform.position, someTransform.rotation);
}

And then, you just put a destroy on the a script's Awake() function which is on the image like so:

var lifetime = 2.0;
function Awake()
{
    Destroy(gameObject, lifetime);
}

Note that I'm assuming you're not spawning hundred thousand enemies per second, so the performance difference between an invoke call (looks for a string) and a co-routine shouldn't matter at all. For the simplest stuff (like doing one thing after some seconds), you shouldn't really need anything other than Invoke().

Well, all those co-routines and waiting for some seconds are nice but, why not just use Invoke() for this purpose as well?

//...
public void SpawnObject (){
    var warningDelay = 5.0;
    Instantiate (spawnee, transform.position, transform.rotation);
    Invoke("CreateWarningImage", warningDelay);
    if (stopSpawning) {
        CancelInvoke ("SpawnObject");
    }

}
//someTransform is where you want the image to be, assuming you've already defined it
public void CreateWarningImage(){
    Instantiate (warning, someTransform.position, someTransform.rotation);
}

And then, you just put a destroy on the a script's Awake() function which is on the image like so:

var lifetime = 2.0;
function Awake()
{
    Destroy(gameObject, lifetime);
}

Well, all those co-routines and waiting for some seconds are nice but, why not just use Invoke() for this purpose as well?

//...
public void SpawnObject (){
    var warningDelay = 5.0;
    Instantiate (spawnee, transform.position, transform.rotation);
    Invoke("CreateWarningImage", warningDelay);
    if (stopSpawning) {
        CancelInvoke ("SpawnObject");
    }

}
//someTransform is where you want the image to be, assuming you've already defined it
public void CreateWarningImage(){
    Instantiate (warning, someTransform.position, someTransform.rotation);
}

And then, you just put a destroy on the a script's Awake() function which is on the image like so:

var lifetime = 2.0;
function Awake()
{
    Destroy(gameObject, lifetime);
}

Note that I'm assuming you're not spawning hundred thousand enemies per second, so the performance difference between an invoke call (looks for a string) and a co-routine shouldn't matter at all. For the simplest stuff (like doing one thing after some seconds), you shouldn't really need anything other than Invoke().

Source Link
John Hamilton
  • 3.1k
  • 1
  • 16
  • 34

Well, all those co-routines and waiting for some seconds are nice but, why not just use Invoke() for this purpose as well?

//...
public void SpawnObject (){
    var warningDelay = 5.0;
    Instantiate (spawnee, transform.position, transform.rotation);
    Invoke("CreateWarningImage", warningDelay);
    if (stopSpawning) {
        CancelInvoke ("SpawnObject");
    }

}
//someTransform is where you want the image to be, assuming you've already defined it
public void CreateWarningImage(){
    Instantiate (warning, someTransform.position, someTransform.rotation);
}

And then, you just put a destroy on the a script's Awake() function which is on the image like so:

var lifetime = 2.0;
function Awake()
{
    Destroy(gameObject, lifetime);
}