Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

127 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Only Crashes in Editor, not in Build App 0 Answers

beginner coder question, is this code right? 1 Answer

Unity freezes MacOS, causes entire system crash 6 Answers

Unity 2017.1f1 Freezes while compiling scripts 3 Answers

What is wrong in my code - Unity freezes 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges