Unity Stuck on EnterPlayMode

My Unity is getting stuck at EnterPlayMode when i press the Play Button. Why does this happen?

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.AI;

public class WalkTowardsCylinder : MonoBehaviour
{
    [SerializeField] private Vector3 StartPos;
    [SerializeField] private NavMeshAgent Agent;
    [SerializeField] private Transform[] Parkinglot1;
    [SerializeField] private Transform[] Parkinglot2;
    [SerializeField] private Transform[] Parkinglot3;
    [SerializeField] private float randomSelection;
    private int waypointindex = 0;

    // Start is called before the first frame update
    void Start()
    {
        Agent = GetComponent<NavMeshAgent>();
        SearchParkinglot();
    }

    // Update is called once per frame
    void Update()
    {

    }

    void SearchParkinglot()
    {
        randomSelection = Random.Range(0,2);
        if (randomSelection == 0)
        {
            while (Vector3.Distance(transform.position, Parkinglot1[waypointindex].position) > 0.2f)
            {
                Agent.SetDestination(Parkinglot1[waypointindex].position);
            }
      
            print("finished");
            Agent.SetDestination(transform.position);
            transform.rotation = Parkinglot1[waypointindex].rotation;
            waypointindex++;
        }
        if (randomSelection == 1)
        {
            while (Vector3.Distance(transform.position, Parkinglot2[waypointindex].position) > 0.2f)
            {
                Agent.SetDestination(Parkinglot2[waypointindex].position);
            }

            print("finished");
             Agent.SetDestination(transform.position);
            transform.rotation = Parkinglot2[waypointindex].rotation;
            waypointindex++;
        }
        if (randomSelection == 2)
        {
            while (Vector3.Distance(transform.position, Parkinglot3[waypointindex].position) > 0.2f)
            {
                Agent.SetDestination(Parkinglot3[waypointindex].position);
            }

            print("finished");
            Agent.SetDestination(transform.position);
            transform.rotation = Parkinglot3[waypointindex].rotation;
            waypointindex++;
        }
    }
}

Usually this means you have an infinite loop. So my guess is it’s hitting one of your while statements and just getting stuck looping forever.

i was reading that in a other forum post, but i don’t know why it’s getting stuck in that loop. it should just go to the position while it’s distance is over 0.2f.

All 3 of your “while” loops are infinite loops. As long as that code is executing, the rest of the engine can’t process a single frame. Therefore NavAgent isn’t moving, therefore the position can’t change, therefore the while condition remains true forever, therefore, infinite loop.

Looking at this, I think your intention is that SearchParkingLot() would execute across time, and act as your AI’s “patrol” loop? If so, it needs to be a coroutine. That looks like this:

void Start()
    {
        Agent = GetComponent<NavMeshAgent>();
        StartCoroutine(SearchParkinglot());
    }

IEnumerator SearchParkingLot() {

And anytime you want the engine to wait for a frame, insert this line:

yield return null;

You need a yield line inside every while loop.

it works! Thank you so much. i need to learn more about unity ^^

i am so confused. i want to make an idle tycoon game and i am making a system to park vehicles at a parkinglot, but i cant find out how to set the parkinglot occupied so that other cars cant choose that parkinglot. Because when i do, the parked car on it just does the same thing as the others. Search a new spot.

or first, to drive up to them and then park

Finally i got it working :slight_smile:

using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.AI;
using Random = UnityEngine.Random;

public class ParkingSystem : MonoBehaviour
{
    [SerializeField] private NavMeshAgent Agent;
    [SerializeField] private NavMeshObstacle Obstacle;
    [SerializeField] private Transform[] Parkinglots;
    private GameObject Customer;
    [SerializeField] private GameObject CustomerPrefab;
    private int rand;

    // Start is called before the first frame update
    void Start()
    {
        Agent = GetComponent<NavMeshAgent>();
        Obstacle = GetComponent<NavMeshObstacle>();
        StartCoroutine(SearchParkingSpot());
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    IEnumerator SearchParkingSpot()
    {
        rand = Random.Range(0, Parkinglots.Length);
        for (int i = 0; i < Parkinglots.Length; i++)
        {
            while (Parkinglots[rand].gameObject.name.Contains("Not"))
            {
                while (Vector3.Distance(Agent.transform.position, Parkinglots[rand].transform.position) <= 0.2f)
                {
                    Parkinglots[rand].gameObject.name = "Occupied";
                    Agent.SetDestination(transform.position);
                    Obstacle.enabled = true;
                    transform.rotation = Parkinglots[rand].rotation;
                    if (transform.rotation == Quaternion.Euler(0f,90f,0f))
                    {
                        Customer = Instantiate(CustomerPrefab,
                            new Vector3(transform.position.x+3, transform.position.y, transform.position.z),
                            Quaternion.identity);
                        Customer.transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y - 90,
                            transform.rotation.z);
                    }else if (transform.rotation == Quaternion.Euler(0f, -90f, 0f))
                    {
                        Customer = Instantiate(CustomerPrefab,
                            new Vector3(transform.position.x-3, transform.position.y, transform.position.z),
                            Quaternion.identity);
                        Customer.transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y + 90,
                            transform.rotation.z);
                    }
                    yield return null;
                }
                Agent.SetDestination(Parkinglots[rand].position);
                yield return null;
            }
            rand = Random.Range(0, Parkinglots.Length);
            Agent.SetDestination(Parkinglots[rand].position);
        }
        yield return null;
    }

   
}

How do you force unity to stop entering play mode when it gets stuck?

Don’t programm infinite loops? Or: https://assetstore.unity.com/packages/tools/utilities/emergency-exit-194959