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. Dismiss Notice

Question Unity Stuck on EnterPlayMode

Discussion in 'Scripting' started by LibRMX, Mar 19, 2021.

  1. LibRMX

    LibRMX

    Joined:
    Aug 5, 2017
    Posts:
    19
    My Unity is getting stuck at EnterPlayMode when i press the Play Button. Why does this happen?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.VisualScripting;
    4. using UnityEngine;
    5. using UnityEngine.AI;
    6.  
    7. public class WalkTowardsCylinder : MonoBehaviour
    8. {
    9.     [SerializeField] private Vector3 StartPos;
    10.     [SerializeField] private NavMeshAgent Agent;
    11.     [SerializeField] private Transform[] Parkinglot1;
    12.     [SerializeField] private Transform[] Parkinglot2;
    13.     [SerializeField] private Transform[] Parkinglot3;
    14.     [SerializeField] private float randomSelection;
    15.     private int waypointindex = 0;
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         Agent = GetComponent<NavMeshAgent>();
    21.         SearchParkinglot();
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.  
    28.     }
    29.  
    30.     void SearchParkinglot()
    31.     {
    32.         randomSelection = Random.Range(0,2);
    33.         if (randomSelection == 0)
    34.         {
    35.             while (Vector3.Distance(transform.position, Parkinglot1[waypointindex].position) > 0.2f)
    36.             {
    37.                 Agent.SetDestination(Parkinglot1[waypointindex].position);
    38.             }
    39.      
    40.             print("finished");
    41.             Agent.SetDestination(transform.position);
    42.             transform.rotation = Parkinglot1[waypointindex].rotation;
    43.             waypointindex++;
    44.         }
    45.         if (randomSelection == 1)
    46.         {
    47.             while (Vector3.Distance(transform.position, Parkinglot2[waypointindex].position) > 0.2f)
    48.             {
    49.                 Agent.SetDestination(Parkinglot2[waypointindex].position);
    50.             }
    51.  
    52.             print("finished");
    53.              Agent.SetDestination(transform.position);
    54.             transform.rotation = Parkinglot2[waypointindex].rotation;
    55.             waypointindex++;
    56.         }
    57.         if (randomSelection == 2)
    58.         {
    59.             while (Vector3.Distance(transform.position, Parkinglot3[waypointindex].position) > 0.2f)
    60.             {
    61.                 Agent.SetDestination(Parkinglot3[waypointindex].position);
    62.             }
    63.  
    64.             print("finished");
    65.             Agent.SetDestination(transform.position);
    66.             transform.rotation = Parkinglot3[waypointindex].rotation;
    67.             waypointindex++;
    68.         }
    69.     }
    70. }
    71.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,138
    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.
     
  3. LibRMX

    LibRMX

    Joined:
    Aug 5, 2017
    Posts:
    19
    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.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    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:
    Code (csharp):
    1.  
    2. void Start()
    3.     {
    4.         Agent = GetComponent<NavMeshAgent>();
    5.         StartCoroutine(SearchParkinglot());
    6.     }
    7.  
    8. IEnumerator SearchParkingLot() {
    And anytime you want the engine to wait for a frame, insert this line:
    Code (csharp):
    1. yield return null;
    You need a yield line inside every while loop.
     
  5. LibRMX

    LibRMX

    Joined:
    Aug 5, 2017
    Posts:
    19

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

    LibRMX

    Joined:
    Aug 5, 2017
    Posts:
    19
    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.
     
  7. LibRMX

    LibRMX

    Joined:
    Aug 5, 2017
    Posts:
    19
    or first, to drive up to them and then park
     
  8. LibRMX

    LibRMX

    Joined:
    Aug 5, 2017
    Posts:
    19
    Finally i got it working :)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.Mathematics;
    4. using Unity.VisualScripting;
    5. using UnityEngine;
    6. using UnityEngine.AI;
    7. using Random = UnityEngine.Random;
    8.  
    9. public class ParkingSystem : MonoBehaviour
    10. {
    11.     [SerializeField] private NavMeshAgent Agent;
    12.     [SerializeField] private NavMeshObstacle Obstacle;
    13.     [SerializeField] private Transform[] Parkinglots;
    14.     private GameObject Customer;
    15.     [SerializeField] private GameObject CustomerPrefab;
    16.     private int rand;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         Agent = GetComponent<NavMeshAgent>();
    22.         Obstacle = GetComponent<NavMeshObstacle>();
    23.         StartCoroutine(SearchParkingSpot());
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.        
    30.     }
    31.  
    32.     IEnumerator SearchParkingSpot()
    33.     {
    34.         rand = Random.Range(0, Parkinglots.Length);
    35.         for (int i = 0; i < Parkinglots.Length; i++)
    36.         {
    37.             while (Parkinglots[rand].gameObject.name.Contains("Not"))
    38.             {
    39.                 while (Vector3.Distance(Agent.transform.position, Parkinglots[rand].transform.position) <= 0.2f)
    40.                 {
    41.                     Parkinglots[rand].gameObject.name = "Occupied";
    42.                     Agent.SetDestination(transform.position);
    43.                     Obstacle.enabled = true;
    44.                     transform.rotation = Parkinglots[rand].rotation;
    45.                     if (transform.rotation == Quaternion.Euler(0f,90f,0f))
    46.                     {
    47.                         Customer = Instantiate(CustomerPrefab,
    48.                             new Vector3(transform.position.x+3, transform.position.y, transform.position.z),
    49.                             Quaternion.identity);
    50.                         Customer.transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y - 90,
    51.                             transform.rotation.z);
    52.                     }else if (transform.rotation == Quaternion.Euler(0f, -90f, 0f))
    53.                     {
    54.                         Customer = Instantiate(CustomerPrefab,
    55.                             new Vector3(transform.position.x-3, transform.position.y, transform.position.z),
    56.                             Quaternion.identity);
    57.                         Customer.transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y + 90,
    58.                             transform.rotation.z);
    59.                     }
    60.                     yield return null;
    61.                 }
    62.                 Agent.SetDestination(Parkinglots[rand].position);
    63.                 yield return null;
    64.             }
    65.             rand = Random.Range(0, Parkinglots.Length);
    66.             Agent.SetDestination(Parkinglots[rand].position);
    67.         }
    68.         yield return null;
    69.     }
    70.  
    71.    
    72. }
    73.  
     
  9. Steven786

    Steven786

    Joined:
    Aug 24, 2021
    Posts:
    1
    How do you force unity to stop entering play mode when it gets stuck?
     
  10. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749