- Home /
Question by
Jonathan07 · Apr 18, 2014 at 08:47 PM ·
loopwhilecondition
While loop doesn't start again when condition is true
Hi, I'm trying to have a heat system for machine guns in my game. It's simple, when you shoot, you produce heat and it doesn't get cooling. When you don't shoot, the cooling occurs. My problem is that when I first shoot, the cooling stop but doesn't happen again. Ever. Here is my code:
using UnityEngine; using System.Collections;
public class GameController : MonoBehaviour {
public float energy, heat, energyGenerated, heatRedux;
public float energyUsed, heatProduced;
public bool IsShootingMG;
void Start(){
StartCoroutine (HeatFunction ());
StartCoroutine (EnergyFunction ());
}
IEnumerator EnergyFunction(){
while(true)
{
yield return new WaitForSeconds(oneSecond);
energy = energy + energyGenerated;
}
}
IEnumerator HeatFunction(){
while(IsShootingMG == false){
yield return new WaitForSeconds(oneSecond);
if(heat > 0){
heat = heat - heatRedux;
if(heat < 0){
heat = 0;
}
}
}
}
Comment
Best Answer
Answer by fafase · Apr 18, 2014 at 08:54 PM
I think you need to wrap your loop in heat function within another while loop that is always running
while(true){
while(IsShootingMG == false){
yield return new WaitForSeconds(oneSecond);
if(heat > 0){
heat = heat - heatRedux;
if(heat < 0){
heat = 0;
}
}
}
yield return null;
}
At the moment if not shooting you quit the method and do not seem to restart it.
Well that actually crashes unity lol. I'll figure something out with that thanks!
Sorry, it freezes unity after a couple seconds actually.
Your answer
