What's the point of using this? Isn't this just like calling a method? Couldn't we just have done something like LaunchProjectile(2);?
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Rigidbody projectile;
void LaunchProjectile() {
Rigidbody instance = Instantiate(projectile);
instance.velocity = Random.insideUnitSphere * 5;
}
public void Awake() {
Invoke("LaunchProjectile", 2);
}
}
Invoke allows you to specify the time when a function is run. You can't do this by calling a function without Invoke unless you write extra code to do that. Also, using Invoke allows you to use CancelInvoke. Again, you can write your own code to handle this, but since Invoke does it already, you might as well use Invoke if the situation calls for it and save yourself the bother.
According to the docs, "Invoke" has two arguments: method to call, and delay in seconds to call the method.
// wait two seconds, after this line is executed, before calling LaunchProjectile
Invoke("LaunchProjectile", 2);
If you to simply pass the number 2 into LaunchProjectile() you will get an error because LaunchProjectile() takes no arguments according to your definition. If you wanted LaunchProjectile() to take the number 2 as an argument for your own devious purposes, you could do something like this:
using UnityEngine;
using System.Collections;
public class MySecretsRevealed: MonoBehaviour {
public int MyDeviousNumber = 2;
void LaunchProjectile( int number ) {
print ("My secret number is " + number + ",shhh! Don't tell!!!");
}
public void Awake() {
LaunchProjectile( MyDeviousNumber );
}
}