I want to make a separate script that using Action action
and a given float time
variable as parameters of course calls multiple functions for the time given ?
Here’s what I’ve done so far
using System.Collections;
using UnityEngine;
using System;
public class Timer : MonoBehaviour
{
private bool doReturn = true;
private float time = 0.0f;
public bool Ability(Action action, float time)
{
StartCoroutine(LateCall(time, action));
return doReturn;
}
private IEnumerator LateCall(float Duration, Action action)
{
action();
yield return new WaitForSeconds(Duration);
doReturn = false;
}
}
I tried shortening the code and focus it on the main problem, which is:
If I call thepublic bool Ability(Action action, float time)
multiple times in another script the fact that it depends on the doReturn
variable which is accessible to all calls I do so for example:
Ability(ExampleFunction, 6);
if(!Ability(ExampleFunction, 6)){
//Do Something
}
Ability(SecondExampleFunction, 2);
if(!Ability(SecondExampleFunction, 2)){
//Do something Else
}
When I run this both functions would stop after 2 seconds because
Ability(SecondExampleFunction, 2);
turns the global variable false after the seconds given, which affects the Ability(ExampleFunction, 6);
.
This problem would be an easy fix if I could use local variables and reference the variables with pointers in the IEnumerator
function, but because I can’t use pointers in unity this is a bigger problem.
So what I want to achieve:
**Make it possible to call random functions using the Ability
function and it shouldn’t be depended on the times I call it, which means
Ability(SecondExampleFunction, 2);
and
Ability(ExampleFunction, 6);
would run in separate timelines and wouldn’t mess with the same variables **