Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Problem with de-bugging

Discussion in 'Scripting' started by Trussmister, May 14, 2024.

  1. Trussmister

    Trussmister

    Joined:
    Mar 24, 2024
    Posts:
    24
    hi, im quite new to unity and please forgive me if i wasted your time, but a piece of code of mine soft locks unity into an eternal state of loading every time i try to run my code, but there isnt any loops in the script that have on end. code attached below, thanks.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.AI.Navigation;
    4. using UnityEngine;
    5. using UnityEngine.AI;
    6.  
    7. public class EnemyLogic : MonoBehaviour
    8. {
    9.     public GameObject Player;
    10.     NavMeshAgent agent;
    11.     public Transform[] waypoints;
    12.     void Start()
    13.     {
    14.         int childnum = 0;
    15.         agent = gameObject.GetComponent<NavMeshAgent>();
    16.         foreach (Transform child in GameObject.Find("Waypoints").transform)
    17.         {
    18.             childnum++;
    19.         }
    20.         waypoints = new Transform[childnum];
    21.         childnum = 0;
    22.         foreach (Transform child in GameObject.Find("Waypoints").transform)
    23.         {
    24.             waypoints[childnum] = child;
    25.             childnum++;
    26.         }
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.         Collider[] colliders = Physics.OverlapSphere(gameObject.transform.position, 25f, layerMask: LayerMask.NameToLayer("Player"));
    33.         if (colliders.Length > 1)
    34.         {
    35.             foreach (Collider collider in colliders)
    36.             {
    37.                 agent.destination = Player.transform.position;
    38.             }
    39.         }
    40.         else
    41.         {
    42.             foreach (Transform waypoint in waypoints)
    43.             {
    44.                 agent.destination = waypoint.position;
    45.                 while (gameObject.transform.position != waypoint.position)
    46.                 {
    47.                    
    48.                 }
    49.             }
    50.         }
    51.     }
    52. }
    53.  
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,390
    Uh, you have a while-loop on line 45 that does nothing. That will just spin on infinitely and Unity will not proceed at all. I imagine this should just be a regular if-statement instead.
     
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,922
    Also don't compare positions, or any float value. Float values may never be precisely identical, this is why Mathf.Approximately exists. But in case of positions you always have to leave greater room by introducing a "close enough" threshold.

    Do this instead:
    Code (CSharp):
    1. var distance = (gameObject.transform.position - waypoint.position).magnitude;
    2. if (distance < 0.1f) { .. }
    The threshold needs to account for the maximum velocity of both objects combined.
     
  4. Trussmister

    Trussmister

    Joined:
    Mar 24, 2024
    Posts:
    24
    Oh, thank you
     
  5. Trussmister

    Trussmister

    Joined:
    Mar 24, 2024
    Posts:
    24
    Thankyou