Jump to content
Unity Insider Forum

Bug:MissingComponentException: There is no 'NavMeshAgent' attached to the "NPC_test" game object, but a script is trying to access it. You probably need to add a NavMeshAgent to the game object "NPC_test". Or your script needs to check if the compone


Recommended Posts

Hey leute, 

ich hab diesen Bug hier bekommen, jedes mal, wenn ich auf ein Interactive Object klicke (Ich hab es so eingestellt, das ich ein Debug Log bekomme, wenn ich dieses Objekt anklicke):

MissingComponentException: There is no 'NavMeshAgent' attached to the "NPC_test" game object, but a script is trying to access it.
You probably need to add a NavMeshAgent to the game object "NPC_test". Or your script needs to check if the component is attached before using it.

Interactable.MoveToInteraction (UnityEngine.AI.NavMeshAgent PlayerAgent) (at Assets/Scripts/Interactable.cs:12)
WorldInteractions.GetInteraction () (at Assets/Scripts/WorldInteractions.cs:30)
WorldInteractions.Update () (at Assets/Scripts/WorldInteractions.cs:18

Das Problem ist aber: ich habe schon einen NavMeshAgent als Player Agent eingestellt (das von meinem Player).

Das hier ist mein Bisheriger Code:

(Hauptcode zum Bewegen)

using System.Collections;
using UnityEngine;
using UnityEngine.AI;

public class WorldInteractions : MonoBehaviour {
    NavMeshAgent PlayerAgent;

    private void Start()
    {
        PlayerAgent = GetComponent<NavMeshAgent>();
    }

        

    void Update()
    {
        if (Input.GetMouseButtonDown(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
        GetInteraction();
    }

    void GetInteraction()
    {
        Ray interactionRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit interactionInfo;
        if (Physics.Raycast(interactionRay, out interactionInfo, Mathf.Infinity))
        {
            GameObject interactedObject = interactionInfo.collider.gameObject;
            if (interactedObject.tag == "Interactable Object")
            {
                interactedObject.GetComponent<Interactable>().MoveToInteraction(PlayerAgent);
            }
            else
            {
                PlayerAgent.destination = interactionInfo.point;
            }
        }
    }
}

(Code zum Interagieren)

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

public class Interactable : WorldInteractions {
    public NavMeshAgent PlayerAgent;

    public virtual void MoveToInteraction(NavMeshAgent PlayerAgent)
    {
        this.PlayerAgent = PlayerAgent;
        PlayerAgent.stoppingDistance = 3f;
        PlayerAgent.destination = this.transform.position;

        Interact();
    }

    public virtual void Interact()
    {
        Debug.Log("Interacting with Base class");
    }
}

(Code zum Ausgeben des Debug-Logs bei NPCs und zum Ausgeben des Debug-Logs bei Items)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class NPC : Interactable {

    public override void Interact()
    {
        Debug.Log("Interacting with NPC");
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class PickupItem : Interactable {
    
    public override void Interact()
    {
        Debug.Log("Interacting with Item!");
    }
}
Link zu diesem Kommentar
Auf anderen Seiten teilen

Kann man pauschal nicht sagen. Vermutlich gibt es aber irgendein Skript, das in Awake oder Start die Komponente löscht. Du musst die Fehlerquelle jetzt nach und nach eingrenzen und finden. Es kann dabei je nach Situation helfen, wenn du von 0 anfängst und nach und nach die Skripts wieder einfügst, bis der Fehler wieder auftaucht.

Link zu diesem Kommentar
Auf anderen Seiten teilen

Archiviert

Dieses Thema ist jetzt archiviert und für weitere Antworten gesperrt.

×
×
  • Neu erstellen...