void Update()
{
if (currentTarget != null)
{
this.Invoke("Shoot(currentTarget)", 0.3f);
}
}
void Shoot(Collider currentTarget)
{
.......
}
I want the Shoot method to be called fast. But all I get is
Trying to Invoke method: Tower.Shoot(currentTarget) couldn't be called.
What can be the problem ?
You cannot call Invoke with parameters. This should work if you remove the parameter from your Shoot function.
Invoke("Shoot", 3f);
Then your shoot function should look like this
void Shoot(){
}
instead of
void Shoot(string...parameter){
}
After your comment, there is another way to do this. That requires "Coroutine".
IEnumerator Shoot(Collider currentTarget, float delayTime)
{
yield return new WaitForSeconds(delayTime);
//You can then put your code below
//......your code
}
You cannot call it directly. For example, you cannot do this:
Shoot(currentTarget, 1f)
;
You must use **StartCoroutine**(Shoot(currentTarget, 1f));
void Start()
{
//Call your function
StartCoroutine(Shoot(currentTarget, 1f));
}
Also if you dont like using StartCoroutine, then you can call the Coroutine function inside another normal function. I think you may like this method so the whole code should look like something below:
//Changed the name to **ShootIEnum**
IEnumerator ShootIEnum(Collider currentTarget, float delayTime=0f)
{
yield return new WaitForSeconds(delayTime);
//You can then put your code below
//......your code
}
//You call this function
void Shoot(Collider currentTarget, float delayTime=0f)
{
StartCoroutine(ShootIEnum(currentTarget, 1f));
}
void Update()
{
if (currentTarget != null)
{
Shoot(currentTarget, 0.3f);
}
}
Now, anytime you want to call Shoot, you can now call Shoot(currentTarget, 1f);
with no problems.