- Home /
Question by
AsclepiiusUnknown · Jun 15, 2020 at 09:35 AM ·
freezecrashingwhile-loop
Unity Crashes From While Loop and Can't fix
I was creating the following state machine for a Uni project and for some reason I can't figure out the solution to the crashing. I know it is being caused by the while loop within each (eg lines 107-131) of the State IEnumerators but can't put my finger on it exactly. Help is greatly appreciated, thanks in advance.
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
[RequireComponent(typeof(Flock))]
public class PreyStateMachine : Life
{
#region Variables
public enum PreyStates
{
Flock,
Wander,
Evade,
Hide,
}
[Header("Prey Variables")]
public bool useHurtColor = true;
public Color hurtColor = Color.red;
[Header("State Machine Variables")]
public PreyStates preyState;
[Header("Timer Values")]
public float waitTime;
private float timer;
[Header("Behaviour Objects")]
[SerializeField] private FlockBehaviour FlockBehaviour;
[SerializeField] private FlockBehaviour WanderBehaviour;
[SerializeField] private FlockBehaviour EvadeBehaviour;
[SerializeField] private FlockBehaviour HideBehaviour;
#endregion
#region Default
protected override void Start()
{
base.Start();
#region Debugging
if (GetComponent<Flock>() != null && flock == null)
{
flock = GetComponent<Flock>();
}
if (flock == null)
{
Debug.LogError("Prey State Machine couldnt find flock");
}
#endregion
ChangeStateTo(preyState.ToString());
}
void Update()
{
stateDisplay.text = preyState.ToString();
if (timer < -1)
{
timer -= Time.deltaTime;
}
}
#endregion
#region Prey Functionality
public void TakeDamage(float damage)
{
health -= damage;
}
public void PlayHurtColorEffectVoid(SpriteRenderer renderer, int repeatAmount, float waitTime)
{
PlayHurtColorEffect(renderer, repeatAmount, waitTime);
}
public IEnumerator PlayHurtColorEffect(SpriteRenderer renderer, int repeatAmount, float waitTime)
{
Color tempStartColor = renderer.color;
for (int i = 0; i < repeatAmount; i++)
{
renderer.color = hurtColor;
yield return new WaitForSeconds(waitTime);
renderer.color = tempStartColor;
}
}
#endregion
#region State Machine Functionality
///
#region Flock
public IEnumerator FlockState()
{
Debug.Log("Flock: ENTER");
preyState = PreyStates.Flock;
flock.behaviour = FlockBehaviour;
//StartCoroutine("DelayStateChangeFW");
timer = waitTime;
while (preyState == PreyStates.Flock)
{
foreach (FlockAgent agent in flock.agents)
{
List<Transform> filteredContext = (contextFilter == null) ? flock.areaContext : contextFilter.Filter(agent, flock.areaContext);
if (filteredContext.Count > 0)
{
//StopCoroutine("DelayStateChangeFW");
timer = -10;
ChangeStateTo("Evade");
preyState = PreyStates.Evade;
yield return null;
}
}
if (timer >= 0)
{
ChangeStateTo("Wander");
preyState = PreyStates.Wander;
yield return null;
}
yield return null;
}
Debug.Log("Flock: EXIT");
}
#endregion
#region Wander
public IEnumerator WanderState()
{
Debug.Log("Wander: ENTER");
preyState = PreyStates.Wander;
flock.behaviour = WanderBehaviour;
//StartCoroutine("DelayStateChangeFW");
timer = waitTime;
while (preyState == PreyStates.Wander)
{
foreach (FlockAgent agent in flock.agents)
{
List<Transform> filteredContext = (contextFilter == null) ? flock.areaContext : contextFilter.Filter(agent, flock.areaContext);
if (filteredContext.Count > 0)
{
//StopCoroutine("DelayStateChangeFW");
timer = -10;
ChangeStateTo("Evade");
preyState = PreyStates.Evade;
yield return null;
}
}
if (timer >= 0)
{
ChangeStateTo("Flock");
preyState = PreyStates.Flock;
yield return null;
}
yield return null;
}
Debug.Log("Wander: EXIT");
}
#endregion
#region Evade
public IEnumerator EvadeState()
{
Debug.Log("Evade: ENTER");
preyState = PreyStates.Evade;
flock.behaviour = EvadeBehaviour;
//StartCoroutine("DelayStateChangeEH");
timer = waitTime;
while (preyState == PreyStates.Evade)
{
foreach (FlockAgent agent in flock.agents)
{
List<Transform> filteredContext = (contextFilter == null) ? flock.areaContext : contextFilter.Filter(agent, flock.areaContext);
if (filteredContext.Count <= 0)
{
//StopCoroutine("DelayStateChangeEH");
timer = -10;
ChangeStateTo("Flock");
preyState = PreyStates.Flock;
yield return null;
}
}
if (timer >= 0)
{
ChangeStateTo("Hide");
preyState = PreyStates.Hide;
yield return null;
}
yield return null;
}
Debug.Log("Evade: EXIT");
}
#endregion
#region Hide
public IEnumerator HideState()
{
Debug.Log("Hide: ENTER");
preyState = PreyStates.Hide;
flock.behaviour = HideBehaviour;
//StartCoroutine("DelayStateChangeEH");
timer = waitTime;
while (preyState == PreyStates.Hide)
{
foreach (FlockAgent agent in flock.agents)
{
List<Transform> filteredContext = (contextFilter == null) ? flock.areaContext : contextFilter.Filter(agent, flock.areaContext);
if (filteredContext.Count <= 0)
{
//StopCoroutine("DelayStateChangeEH");
timer = -10;
ChangeStateTo("Flock");
preyState = PreyStates.Flock;
yield return null;
}
}
if (timer >= 0)
{
ChangeStateTo("Evade");
preyState = PreyStates.Evade;
yield return null;
}
yield return null;
}
Debug.Log("Evade: EXIT");
}
#endregion
#region State Changing
void ChangeStateTo(string methodName)
{
StartCoroutine(methodName + "State"); //change the state to the passed value plus "State"
}
/*public IEnumerator DelayStateChangeFW()
{
Debug.Log("Counting...");
yield return new WaitForSeconds(10);
if (preyState == PreyStates.Flock)
{
ChangeStateTo("Wander");
//yield return null;
}
else
{
ChangeStateTo("Flock");
}
}
public IEnumerator DelayStateChangeEH()
{
Debug.Log("Counting...");
yield return new WaitForSeconds(10);
if (preyState == PreyStates.Evade)
{
ChangeStateTo("Hide");
}
else
{
ChangeStateTo("Evade");
}
}*/
#endregion
#endregion
}
Comment