Local bool won't change inside coroutine
EDIT: I removed networking from title since I was able to reproduce this in a regular monobehaviour. So it's all down to regular Coroutines not being able to change a variable.
The bool "skill1OnCD" won't change when passed to a coroutine.
private bool skill1OnCD;
void Start(){
skill1OnCD = false;
}
void Update()
{
if (isLocalPlayer)
{
if (Input.GetKeyDown(skillKey1))
{
Debug.LogError(skill1OnCD);
if (!skill1OnCD)
{
string skillName = skillDatabase.skills[0].displayName;
ActivateSkill(skillName);
skill1OnCD = true;
StartCoroutine(ReduceCooldown(skillDatabase.skills[0].cooldown, skill1cdImage, skill1OnCD));
}
else
{
Debug.LogError("Skill is on CD");
}
}
}
IEnumerator ReduceCooldown(float duration, Image skillCD, bool onCD)
{
if (!isLocalPlayer)
{
yield break;
}
Debug.LogError("Started cooldown for " + duration + " seconds");
skillCD.fillAmount = 1f;
float stopTime = Time.realtimeSinceStartup + duration;
while(Time.realtimeSinceStartup <= stopTime)
{
float lerpValue = Mathf.Lerp(skillCD.fillAmount, (stopTime - Time.realtimeSinceStartup) / stopTime, Time.deltaTime * 10f);
skillCD.fillAmount = lerpValue;
yield return new WaitForEndOfFrame();
}
skillCD.fillAmount = 0f;
Debug.LogError("Cooldown finished, setting CD to false");
onCD = false;
Debug.LogError(onCD);
}
So, the Debug error in the coroutine says that the variable onCD has been set to false. When I try to press the key again the debug error says the variable is true. The script inherit NetworkBehaviour, is this some weird sync error? The variable isn't set to Synv so I dont get why it would.
Answer by Salmjak · Feb 15, 2016 at 04:18 PM
Once I removed the NetworkBehaviour from my Google searches it was quite easy to find an answer. Bool is a Value type and does not pass a reference. Credit to @Baste and @NoseKills
A solution to the problem can be found here.
Wrong; this has nothing to do with ReduceCooldown being a coroutine, and all to do with skill1OnCD being a bool. Bool variables are always passed by value. You can try to do this:
void Awake() {
SetToTrue(skill1OnCD);
print(skill1OnCD);
}
void SetToTrue(bool b) {
b = true;
}
The print-call will print "false".
@Baste so this is the case for all value types? Have I really just been lucky (or unlucky depending on how you see it) that I've never ran into this problem before?
So this should be the same for:
int num = 0;
void Awake(){
Increment(num);
print(num);
}
void Increment(int i){
i++;
}
Yes, that will still print 0.
You could pass by reference;
void Awake() {
Increment(ref num);
print(num); //prints 1
}
void Increment(ref int i) {
i++;
}
But that's not possible in an IEnumerator (it won't compile)
Your answer
Follow this Question
Related Questions
StartCoroutine not listening to parameters 1 Answer
WaitForSeconds doesn't wait if time is too low 0 Answers
Why doesn't the collider work? 1 Answer
Weird problem with simple boolean and "if" statement [C#] 0 Answers
animator boolean 2 Answers