Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Enemy Not Found - NavMeshAgent

Discussion in 'Scripting' started by thiagoCarelli, Mar 20, 2023.

  1. thiagoCarelli

    thiagoCarelli

    Joined:
    Mar 11, 2023
    Posts:
    2
    good afternoon,

    I'm having trouble getting my character to be chased by the enemy (navMeshAgent), I have an area of range for the enemy to be active, and it works, but the enemy is running still, I believe that's the problem.

    "player = GameObject.FindGameObjectWithTag("Player").transform;"

    I tried in different ways to correct the code but nothing was possible, the BAKE of the mesh is correct and the animations too, I also keep getting this error on the screen that I couldn't find out what it refers to:

    NullReferenceException: Object reference not set to an instance of an object
    UnityEditor.Graphs.Edge.WakeUp () (at <834c608fccf644818e20b78d1d66c782>:0)
    UnityEditor.Graphs.Graph.DoWakeUpEdges (System.Collections.Generic.List`1[T] inEdges, System.Collections.Generic.List`1[T] ok, System.Collections.Generic.List`1[T] error, System.Boolean inEdgesUsedToBeValid) (at <834c608fccf644818e20b78d1d66c782>:0)
    UnityEditor.Graphs.Graph.WakeUpEdges (System.Boolean clearSlotEdges) (at <834c608fccf644818e20b78d1d66c782>:0)
    UnityEditor.Graphs.Graph.WakeUp (System.Boolean force) (at <834c608fccf644818e20b78d1d66c782>:0)
    UnityEditor.Graphs.Graph.WakeUp () (at <834c608fccf644818e20b78d1d66c782>:0)
    UnityEditor.Graphs.Graph.OnEnable () (at <834c608fccf644818e20b78d1d66c782>:0)



    follow enemy code


    Code (CSharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.AI;
    7.  
    8. public class Enemys : MonoBehaviour
    9. {
    10.  
    11.     [Header("Atributos")]
    12.     public float vida;
    13.     public float dano;
    14.     public float speed;
    15.     public float lookRadius;
    16.     public float colliderRadius = 2f;
    17.  
    18.  
    19.     [Header("Componentes")]
    20.     public Animator anim;
    21.     public CapsuleCollider capsule;
    22.     public NavMeshAgent agent;
    23.  
    24.  
    25.     [Header("Others")]
    26.     public Transform player;
    27.     public bool attacking;
    28.     public bool walking;
    29.  
    30.  
    31.     // Start is called before the first frame update
    32.     void Start()
    33.     {
    34.         anim = GetComponent<Animator>();
    35.         capsule = GetComponent<CapsuleCollider>();
    36.         agent = GetComponent<NavMeshAgent>();
    37.  
    38.         player = GameObject.FindGameObjectWithTag("Player").transform;
    39.     }
    40.  
    41.     // Update is called once per frame
    42.     void Update()
    43.     {
    44.         player = GameObject.FindGameObjectWithTag("Player").transform;
    45.         float distance = Vector3.Distance(player.position, transform.position);
    46.  
    47.         if (distance <= lookRadius)
    48.         {
    49.  
    50.             //agent.isStopped = false;
    51.  
    52.             agent.SetDestination(player.position);
    53.             if (!attacking)
    54.             {
    55.                 agent.SetDestination(player.position);
    56.                 anim.SetBool("walk", true);
    57.                 walking = true;
    58.             }
    59.  
    60.             if (distance <= agent.stoppingDistance)
    61.             {
    62.                 //agent.isStopped = true;
    63.                 StartCoroutine("Attack");
    64.             }
    65.             else
    66.             {
    67.                 //attacking = false;
    68.             }
    69.         }
    70.         else
    71.         {
    72.             //agent.isStopped = true;
    73.             anim.SetBool("walk", false);
    74.             walking = false;
    75.             attacking = false;
    76.         }
    77.     }
    78.  
    79.  
    80.     IEnumerator Attack()
    81.     {
    82.         attacking = true;
    83.         walking = false;
    84.         anim.SetBool("walk", false);
    85.         anim.SetBool("attack", true);
    86.        
    87.         yield return new WaitForSeconds(1f);
    88.         GetPlayer();
    89.  
    90.         //yield return new WaitForSeconds(1f);
    91.     }
    92.  
    93.     void GetPlayer()
    94.     {
    95.         foreach (Collider c in Physics.OverlapSphere((transform.position + transform.forward * colliderRadius), colliderRadius))
    96.         {
    97.             if (c.gameObject.CompareTag("Player"))
    98.             {
    99.             }
    100.         }
    101.     }
    102.  
    103.  
    104.     private void OnDrawGizmosSelected()
    105.     {
    106.         Gizmos.color = Color.red;
    107.         Gizmos.DrawWireSphere(transform.position, lookRadius);
    108.     }
    109.  
    110. }
    111.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
    This error, coming from this location (eg, NOT your code), is often fixed by either Reset Layouts (upper right) or else Right-click Project, Reimport-All.

    NullReferenceException: Object reference not set to an instance of an object
    UnityEditor.Graphs.Edge.WakeUp ()

    As to this problem:

    You might want to look at your GetPlayer() function, which to my reading does nothing.

    If that's not it, time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    When in doubt, print it out!(tm)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.