Erreur avec le targetnode

Pour les scripts écrits en C#
Règles du forum
Merci de respecter la NOMENCLATURE suivante pour vos TITRES de messages :

Commencez par le niveau de vos scripts
DB = Débutant
MY = Moyen
CF = Confirmé

Puis le domaine d'application
-RS = Réseau
-AL = Algorithmie

Exemple :

[DB-RS] Mouvement perso multijoueur
Pompeignus
Messages : 8
Inscription : 15 Mars 2020 10:25

Erreur avec le targetnode

Message par Pompeignus » 15 Mars 2020 13:32

[DB-AL] Erreur avec le targetNode du civil a partir de ce code:

Code : Tout sélectionner

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

public class Civilian : MonoBehaviour {

    public TaskList task;
    public RessourceManager RM;

    private ActionList AL;

    GameObject targetNode;

    public NodeManager.ResourceTypes heldResourceType;
    public bool isGathering = false;

    private NavMeshAgent agent;

    public int heldResource;
    public int maxHeldResource;
    public GameObject[] drops;

    // Use this for initialization
    void Start () {
        StartCoroutine(GatherTick());
        agent = GetComponent<NavMeshAgent>();
        AL = FindObjectOfType<ActionList>();
    }
	
	// Update is called once per frame
	void Update () {
        if (targetNode == null)
        {
            if (heldResource != 0)
            {
                drops = GameObject.FindGameObjectsWithTag("Drops");
                agent.destination = GetClosestDropOff(drops).transform.position;
                drops = null;
                task = TaskList.Delivering;
            }
            else
            {
                task = TaskList.Idle;
            }
        }
        if (heldResource >= maxHeldResource)
        {
            //Drop off point here
            drops = GameObject.FindGameObjectsWithTag("Drops");
            agent.destination = GetClosestDropOff(drops).transform.position;
            drops = null;
            task = TaskList.Delivering;
        }
        if (Input.GetMouseButtonDown(1) && GetComponent<ObjectInfo>().isSelected)
        {
            RightClick();
        }
    }

    GameObject GetClosestDropOff(GameObject[] dropOffs)
    {
        GameObject closestDrop = null;
        float closestDistance = Mathf.Infinity;
        Vector3 position = transform.position;

        foreach (GameObject targetDrop in dropOffs)
        {
            Vector3 direction = targetDrop.transform.position - position;
            float distance = direction.sqrMagnitude;
            if (distance < closestDistance)
            {
                closestDistance = distance;
                closestDrop = targetDrop;
            }
        }

        return closestDrop;
    }

    public void RightClick()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 100))
        {
            if (hit.collider.tag == "Ground")
            {

                AL.Move(agent, hit, task);
            }
            else if (hit.collider.tag == "Ressource")
            {

                AL.Harvest(agent, hit, task, targetNode);
            }
        }
    }

    public void OnTriggerEnter(Collider other)
    {
        GameObject hitObject = other.gameObject;

        if (hitObject.tag == "Ressource" && task == TaskList.Gathering)
        {
            isGathering = true;
            hitObject.GetComponent<NodeManager>().gatherers++;
            heldResourceType = hitObject.GetComponent<NodeManager>().resourceType;
        }
        else if (hitObject.tag == "Drops" && task == TaskList.Delivering)
        {
            if (RM.stone >= RM.maxStone)
            {
                task = TaskList.Idle;
            }
            else
            {
                RM.stone += heldResource;
                heldResource = 0;
                task = TaskList.Gathering;
                agent.destination = targetNode.transform.position;
            }
        }
    }


    public void OnTriggerExit(Collider other)
    {
        GameObject hitObject = other.gameObject;

        if (hitObject.tag == "Ressource")
        {

            hitObject.GetComponent<NodeManager>().gatherers--;
            isGathering = false;
        }
    }
    IEnumerator GatherTick()
    {
        while (true)
        {
            yield return new WaitForSeconds(1);
            if (isGathering)
            {
                heldResource++;
            }

        }
    }
}
Je ne vois pas ou c est l erreur...quelle est la solution?

Avatar de l’utilisateur
Aelhan
Messages : 124
Inscription : 11 Déc 2019 23:00

Re: Erreur avec le targetnode

Message par Aelhan » 15 Mars 2020 13:58

Bonjour (la bienséance veut que la plupart des posts commencent ainsi),

Peux tu nous dire quelle est l'erreur rencontrée (celle que renvoie la console) ? Ce sera bien plus facile pour tout le monde.
Celui qui pose une question risque cinq minutes d'avoir l'air bête.
Celui qui ne pose pas de question restera bête toute sa vie.

Pompeignus
Messages : 8
Inscription : 15 Mars 2020 10:25

Re: Erreur avec le targetnode

Message par Pompeignus » 15 Mars 2020 14:38

Quand je sélection le civil,il ne ramasse pas les ressources et il ne s affiche pas l icone du civil lorsque je le sélection... il me marque ceci : Assets/Script/Civilian.cs(13,16): warning CS0649: Field `Civilian.targetNode' is never assigned to, and will always have its default value `null'

Avatar de l’utilisateur
Max
Messages : 8763
Inscription : 30 Juil 2011 13:57
Contact :

Re: Erreur avec le targetnode

Message par Max » 15 Mars 2020 15:09

Bonjour,

Tes explications sont vraiment vagues, et ne permettent pas vraiment de se faire une idée réelle de ton soucis.
D’où sort le code ? il y a pas mal de dépendance visiblement.
Sinon, ton soucis au niveau du targetNode est peut-être du au fait qu'il reste à null. As-tu bien des objets avec le tag "Drops" ?
Image
Pas d'aide par MP, le forum est là pour ça.
En cas de doute sur les bonnes pratiques à adopter sur le forum, consulter la Charte et sa FAQ

Avatar de l’utilisateur
Aelhan
Messages : 124
Inscription : 11 Déc 2019 23:00

Re: Erreur avec le targetnode

Message par Aelhan » 15 Mars 2020 17:36

`Civilian.targetNode' is never assigned to, and will always have its default value `null'

En effet, ton GameObject targetNode est privé n'est jamais assigné, donc forcément ça pose des problèmes à Unity, surtout que dans ton script tu essaies d'utiliser son transform à un moment.
Donc tu as 2 solutions, soit tu le passes en public et tu lui assignes un GameObject (via inspector ou un autre script) ou tu le laisses en privé, mais il faut que dans ton Start() tu lui assignes quelque chose (et éventuellement selon les conditions et tes besoins, lui assigner autre chose dynamiquement dans une autre fonction).
Celui qui pose une question risque cinq minutes d'avoir l'air bête.
Celui qui ne pose pas de question restera bête toute sa vie.

Pompeignus
Messages : 8
Inscription : 15 Mars 2020 10:25

Re: Erreur avec le targetnode

Message par Pompeignus » 19 Mars 2020 11:58

[DB-AL]

Code : Tout sélectionner

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

public class Civilian : MonoBehaviour {

    public TaskList task;
    public RessourceManager RM;

    private ActionList AL;

    public GameObject targetNode;

    public NodeManager.ResourceTypes heldResourceType;
    public bool isGathering = false;

    private NavMeshAgent agent;

    public int heldResource;
    public int maxHeldResource;
    public GameObject[] drops;

    public float distToTarget;

    public bool isGatherer = false;

    // Use this for initialization
    void Start () {
        StartCoroutine(GatherTick());
        agent = GetComponent<NavMeshAgent>();
        AL = FindObjectOfType<ActionList>();
    }
	
	// Update is called once per frame
	void Update () {


        if (task == TaskList.Gathering)
        {
            distToTarget = Vector3.Distance(transform.position, targetNode.transform.position);
            if (distToTarget <= 3.5f)
            {
                Gather();
            }
        }

        if(task == TaskList.Delivering)
        {
            if(distToTarget <= 3.5f)
            {
                if(RM.stone >= RM.maxStone)
                {
                    task = TaskList.Idle;
                }
                else
                {
                    RM.stone += heldResource;
                    heldResource = 0;
                    task = TaskList.Gathering;
                    agent.destination = targetNode.transform.position;
                    isGatherer = false;
                }
            }
        }

        if (targetNode == null)
        {
            if (heldResource != 0)
            {
                drops = GameObject.FindGameObjectsWithTag("Drops");
                agent.destination = GetClosestDropOff(drops).transform.position;
                distToTarget = Vector3.Distance(GetClosestDropOff(drops).transform.position, transform.position);
                drops = null;
                task = TaskList.Delivering;
            }
            else
            {
                task = TaskList.Idle;
            }
        }
        if (heldResource >= maxHeldResource)
        {
            //Drop off point here
            targetNode.GetComponent<NodeManager>().gatherers--;
            isGathering = false;
            drops = GameObject.FindGameObjectsWithTag("Drops");
            agent.destination = GetClosestDropOff(drops).transform.position;
            distToTarget = Vector3.Distance(GetClosestDropOff(drops).transform.position, transform.position);
            drops = null;
            task = TaskList.Delivering;
            GetComponent<NavMeshObstacle>().enabled = false;
            GetComponent<NavMeshAgent>().enabled = true;
        }
        if (Input.GetMouseButtonDown(1) && GetComponent<ObjectInfo>().isSelected)
        {
            RightClick();
        }
    }

    GameObject GetClosestDropOff(GameObject[] dropOffs)
    {
        GameObject closestDrop = null;
        float closestDistance = Mathf.Infinity;
        Vector3 position = transform.position;

        foreach (GameObject targetDrop in dropOffs)
        {
            Vector3 direction = targetDrop.transform.position - position;
            float distance = direction.sqrMagnitude;
            if (distance < closestDistance)
            {
                closestDistance = distance;
                closestDrop = targetDrop;
            }
        }

        return closestDrop;
    }

    public void RightClick()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 100))
        {
            if (hit.collider.tag == "Ground")
            {
                targetNode.GetComponent<NodeManager>().gatherers--;
                isGathering = false;
                AL.Move(agent, hit, task);
                task = TaskList.Moving;
                GetComponent<NavMeshObstacle>().enabled = false;
                GetComponent<NavMeshAgent>().enabled = true;
            }
            else if (hit.collider.tag == "Ressource")
            {

                AL.Move(agent, hit, task);
                targetNode = hit.collider.gameObject;
                task = TaskList.Gathering;
            }

            else if (hit.collider.tag == "Drops")
            {
                targetNode.GetComponent<NodeManager>().gatherers--;
                isGathering = false;
                drops = GameObject.FindGameObjectsWithTag("Drops");
                agent.destination = GetClosestDropOff(drops).transform.position;
                distToTarget = Vector3.Distance(GetClosestDropOff(drops).transform.position, transform.position);
                drops = null;
                task = TaskList.Delivering;
                GetComponent<NavMeshObstacle>().enabled = false;
                GetComponent<NavMeshAgent>().enabled = true;
            }
        }
    }

    public void Gather()
    {
        isGathering = true;
        if (!isGatherer)
        {
            targetNode.GetComponent<NodeManager>().gatherers++;
            isGatherer = true;
        }
        heldResourceType = targetNode.GetComponent<NodeManager>().resourceType;
        GetComponent<NavMeshObstacle>().enabled = true;
        GetComponent<NavMeshAgent>().enabled = false;
        
    }

    //public void OnTriggerEnter(Collider other)
    //{
    //    GameObject hitObject = other.gameObject;

    //    if (hitObject.tag == "Ressource" && task == TaskList.Gathering)
    //    {
    //        isGathering = true;
    //        hitObject.GetComponent<NodeManager>().gatherers++;
    //        heldResourceType = hitObject.GetComponent<NodeManager>().resourceType;
    //        GetComponent<NavMeshObstacle>().enabled = true;
    //        GetComponent<NavMeshAgent>().enabled = false;
    //    }
    //    else if (hitObject.tag == "Drops" && task == TaskList.Delivering)
    //    {
    //        if (RM.stone >= RM.maxStone)
    //        {
    //            task = TaskList.Idle;
    //        }
    //        else
    //        {
    //            RM.stone += heldResource;
    //            heldResource = 0;
    //            task = TaskList.Gathering;
    //            agent.destination = targetNode.transform.position;
    //        }
    //    }
    //}


    IEnumerator GatherTick()
    {
        while (true)
        {
            yield return new WaitForSeconds(1);
            if (isGathering)
            {
                heldResource++;
            }

        }
    }
}

J ai deux problème;
1) il me dit ceci : "SetDestination" can only be called on an active agent that has been placed on a NavMesh.
UnityEngine.AI.NavMeshAgent:set_destination(Vector3)
Civilian:Update() (at Assets/Script/Civilian.cs:88)
2) l autre me dit ceci : NavMeshAgent and NavMeshObstacle components are active at the same time. This can lead to errorneous behavior.

Je ne sais pas ou c est l erreur...Est-ce que vous avez une ou des idée(s)?Si oui, lequelles?

Avatar de l’utilisateur
Max
Messages : 8763
Inscription : 30 Juil 2011 13:57
Contact :

Re: Erreur avec le targetnode

Message par Max » 19 Mars 2020 12:28

Bonjour,

question bête: as-tu bien un NavMeshAgent (et actif) sur ton GameObject ?
Image
Pas d'aide par MP, le forum est là pour ça.
En cas de doute sur les bonnes pratiques à adopter sur le forum, consulter la Charte et sa FAQ

Pompeignus
Messages : 8
Inscription : 15 Mars 2020 10:25

Re: Erreur avec le targetnode

Message par Pompeignus » 19 Mars 2020 12:56

oui

Avatar de l’utilisateur
Max
Messages : 8763
Inscription : 30 Juil 2011 13:57
Contact :

Re: Erreur avec le targetnode

Message par Max » 19 Mars 2020 14:20

A mon avis tu cherches à donner une destination à ton NavMeshAgent et en même temps tu dois jouer sur le tranform par un repositionnement du GameObject.
Image
Pas d'aide par MP, le forum est là pour ça.
En cas de doute sur les bonnes pratiques à adopter sur le forum, consulter la Charte et sa FAQ

Pompeignus
Messages : 8
Inscription : 15 Mars 2020 10:25

Re: Erreur avec le targetnode

Message par Pompeignus » 19 Mars 2020 15:34

Je ne sais pas mais j ai un autre problème , est-ce que vous trouvez bizarre que le civil se déplace tout seul?(sans animation) Quelle est la solution?

Répondre

Revenir vers « (C#) CSharp »