Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Need a little help with a while loop

Discussion in 'Scripting' started by sadsack, Oct 30, 2019.

  1. sadsack

    sadsack

    Joined:
    May 27, 2015
    Posts:
    156
    I working on a FSM script.
    This is the error:
    The body of `FSM_script.Start()' cannot be an iterator block because `void' is not an iterator interface type
    When I used the while loop while(true) and at the end of the while loop yield return 0;
    I am using Unity 5.6 0n windows 7
    I hope some one can help

    here the code.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FSM_script : MonoBehaviour
    6. {
    7.     public enum State
    8.     {
    9.         Idle,
    10.         Initialize,
    11.         Setup,
    12.         Actions
    13.  
    14.     }
    15.  
    16.     private State _state;  //this is the local variable that reptesents our states
    17.  
    18.     // Use this for initialization
    19.     void Start ()
    20.     {
    21.         _state = State.Initialize;
    22.        
    23.         while(true){  
    24.             switch (_state)
    25.             {
    26.  
    27.             case State.Initialize:
    28.                 InitMe ();
    29.  
    30.                 break;
    31.             case State.Setup:
    32.                 break;
    33.  
    34.             }
    35.  
    36.             yeild return 0;
    37.         }
    38.  
    39.     }
    40.  
    41.     private void InitMe()
    42.     {
    43.         Debug.Log("FSM is Initialized");
    44.         _state = State.Setup;
    45.  
    46.     }
    47.  
    48.     private void SetMeUp()
    49.     {
    50.         Debug.Log("this is SetMeUp function");
    51.     }
    52.  
    53. }
    54.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,742
    Make Start an IEnumerator instead of a void if you want Unity to treat it as a coroutine.

    Also, do you have a particular reason for using such an old version of Unity?
     
  3. sadsack

    sadsack

    Joined:
    May 27, 2015
    Posts:
    156
    I will look up IEnumerator and see how to use it. I have dial up type service. It will take a day or more to download a newer one.
     
  4. sadsack

    sadsack

    Joined:
    May 27, 2015
    Posts:
    156
    semicolon does not help
     
  5. DominoM

    DominoM

    Joined:
    Nov 24, 2016
    Posts:
    460
    Yeah.. Had a confused moment and didn't hit delete fast enough. Sorry about that :)
     
  6. sadsack

    sadsack

    Joined:
    May 27, 2015
    Posts:
    156
    No problem I am 75, I have a lot of confused moments
     
  7. sadsack

    sadsack

    Joined:
    May 27, 2015
    Posts:
    156
    ok I looked it up
    what I understand is use IEnumerator like this IEnumerator Start ()

    Now do I have to use it on all my function?
     
  8. DominoM

    DominoM

    Joined:
    Nov 24, 2016
    Posts:
    460
    Should your class be inheriting from monobehavior?
     
  9. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
  10. sadsack

    sadsack

    Joined:
    May 27, 2015
    Posts:
    156
    I am using a tut.
    .
    I will check your site out. Well I can see this is a not getting me any place
    I will go look for another tut. It worked on his computer.
    Thank you all for your help.
     
  11. DominoM

    DominoM

    Joined:
    Nov 24, 2016
    Posts:
    460
    Cool. I didn't know about using event hooks as IEnumerators like that.

    If you double check the tutorial from 10:33 it should help.

    I got your code to work by changing two lines:

    19. IEnumerator Start()
    36. yield return 0;
     
  12. sadsack

    sadsack

    Joined:
    May 27, 2015
    Posts:
    156
    I am going to download some more tut. I will make me a great FSM
     
  13. sadsack

    sadsack

    Joined:
    May 27, 2015
    Posts:
    156
    Domino did you get it to work???
     
  14. DominoM

    DominoM

    Joined:
    Nov 24, 2016
    Posts:
    460
    Yes, I posted the changes in the spoiler in my previous comment.
     
  15. sadsack

    sadsack

    Joined:
    May 27, 2015
    Posts:
    156
    Ok, guys I found the problems, I have more than one. My eyes are not as good as they use to be.
    I got it working up to this point
    Thank you all
    Renny
     
  16. sadsack

    sadsack

    Joined:
    May 27, 2015
    Posts:
    156
    I did not see the spoiler. must be something new.
     
  17. sadsack

    sadsack

    Joined:
    May 27, 2015
    Posts:
    156
    Well the deed is done, I got the FSM working just like it should be working
    WOW!!!!!!
    Thanks
     
    DominoM likes this.