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 İnfinite Loop Or Random parameter Error? play mode keeps crashing

Discussion in 'Scripting' started by dublajar, Jul 16, 2023.

  1. dublajar

    dublajar

    Joined:
    Apr 24, 2023
    Posts:
    12
    Soo well, my game keeps freezing, generally when there is 2 or less values left in the shelfList, i dont know the reason but people say maybe an infinite loop or the Random parameter. i need help



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. using UnityEngine.Rendering;
    6.  
    7. public class Customer : MonoBehaviour
    8. {
    9.     public float _countdown;
    10.  
    11.     GameObject target;
    12.  
    13.     NavMeshAgent agent;
    14.  
    15.     GameManager gameManager;
    16.     public GameObject GM;
    17.  
    18.     int shelfCount;
    19.  
    20.     public List<int> shelfList = new List<int>();
    21.  
    22.     bool agentMoving;
    23.     bool agentWaiting;
    24.  
    25.     private void Awake()
    26.     {
    27.         agent = GetComponent<NavMeshAgent>();
    28.         gameManager = GM.GetComponent<GameManager>();
    29.         shelfCount = gameManager.currentShopShelfCount;
    30.  
    31.         StartAgent();
    32.     }
    33.  
    34.     void StartAgent()
    35.     {
    36.         for (int i = 0; i < shelfCount; i++)
    37.         {
    38.             shelfList.Add(i);
    39.             print(i);
    40.         }
    41.         if (shelfList.Count == shelfCount)
    42.         {
    43.             NextTarget();
    44.         }
    45.     }
    46.  
    47.     void NextTarget()
    48.     {
    49.         print("NextTarget");
    50.         int i = Random.Range(1, shelfList.Count + 1);
    51.         if (shelfList.Contains(i-1))
    52.         {
    53.             print(i);
    54.             target = gameManager.currentShop.transform.Find("Raflar").GetChild(i - 1).GetChild(0).gameObject;
    55.             shelfList.Remove(i-1);
    56.             agentMoving = true;
    57.         }
    58.         else
    59.         {
    60.             NextTarget();
    61.         }
    62.     }
    63.  
    64.     void RandomCountDown()
    65.     {
    66.         _countdown = Random.Range(5, 20);
    67.     }
    68.  
    69.     public void Update()
    70.     {
    71.         if (shelfList.Count == 0)
    72.         {
    73.             target = null;
    74.             agent.destination = gameManager.currentShop.transform.Find("MarketExit").gameObject.transform.position;
    75.         }
    76.         if (agentMoving)
    77.         {
    78.             print("AgentMoving");
    79.             agent.destination = target.transform.position;
    80.             if (Vector3.Distance(transform.position,target.transform.position) < 2)
    81.             {
    82.                 agentMoving = false;
    83.                 agentWaiting = true;
    84.                 RandomCountDown();
    85.                 print("AgentGone");
    86.             }
    87.         }
    88.         if (agentWaiting)
    89.         {
    90.             _countdown -= Time.deltaTime;
    91.             if (_countdown < 1)
    92.             {
    93.                 print("CountDownOff");
    94.                 NextTarget();
    95.                 agentWaiting = false;
    96.             }
    97.         }
    98.  
    99.         if (Input.GetKeyDown("n"))
    100.         {
    101.             NextTarget();
    102.         }
    103.     }
    104. }
    105.  
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Your infinite loop is happening at line 60. You need to find a better way of choosing your next target.
     
    Yoreki likes this.
  3. dublajar

    dublajar

    Joined:
    Apr 24, 2023
    Posts:
    12
    alright will change today thanks alot man, do you have any suggestions maybe?
     
  4. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    I dunno.. The way you're managing your shelf seems a little confusing and so I'm not sure what to suggest. But you should avoid looping when choosing things at random as it's very inefficient.

    Try something like this:

    Code (CSharp):
    1.     void NextTarget()
    2.     {
    3.         print("NextTarget");
    4.         int i = Random.Range(0, shelfList.Count);
    5.         print(i);
    6.         target = gameManager.currentShop.transform.Find("Raflar").GetChild(i).GetChild(0).gameObject;
    7.         shelfList.RemoveAt(i);
    8.         agentMoving = true;
    9.     }
    Or if Raflar holds the items as child objects then why not just target a random child object directly instead of going through a shelf?.
     
    Last edited: Jul 16, 2023
  5. dublajar

    dublajar

    Joined:
    Apr 24, 2023
    Posts:
    12
    Raflar means shelfes in my language sorry for that lol, yes "Raflar" holds the shelf gameobjects, all the four shelf's gameobjets are child of Raflar, so in different markets i can count how many there are since they maybe rnaomdly generated
     
  6. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510