[resolu]erreur étrange "not all code paths return a value"

Questions à propos du scripting. Hors Shader, GUI, Audio et Mobile.
Avatar de l’utilisateur
poupoule_h5n1
Messages : 48
Inscription : 28 Nov 2022 20:24

[resolu]erreur étrange "not all code paths return a value"

Message par poupoule_h5n1 » 19 Fév 2023 03:00

Bien le bonjour, je code un petit jeu de cuisine avec une génération aléatoire de client. (je débute, je sais que mon code peut être plus optimisé). Malheureusement, une erreur me bloque. "not all code paths return a value"
J'ai fait une petite recherche a se propos et j'ai crus comprendre que cette erreur survient quant on fait un if sans condition de retour, or je ne pense pas que cela peu s'appliquer a mon code.
voici le code en question :

Code : Tout sélectionner

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

public class AnimClient : MonoBehaviour
{
    public Animator blue;
    public Animator red;
    public Animator doctor;
    public int attente;
    public int NombreClient;
    public GameObject ClientBlue;
    public GameObject ClientRed;
    public GameObject ClientDocteur;
    public GameObject Wp;
    int MaxClientDifférents = 4;
    int pose0 = 0;
    int pose1 = 0;
    int pose2 = 0;
    int pose3 = 0;
    int pose4 = 0;
    bool wait = true;

    void Start()
    {
        StartCoroutine(Spawn());
    }

    void Update()
    {
        if(pose0 != 0 && pose1 == 0 && !wait)
        {
            StartCoroutine(Trajet1(pose0));
        }
        if(pose1 !=0 && pose2 == 0 && !wait)
        {
            StartCoroutine(Trajet2(pose1));
        }
        if (pose2 != 0 && pose3 == 0 && !wait)
        {
            StartCoroutine(Trajet3(pose2));
        }
        if (pose3 !=0 && pose4 == 0 && !wait)
        {
            StartCoroutine(Trajet4(pose3));
        }
    }

    IEnumerator Spawn()
    {
    // c'est ici que l'erreur se situe.
        wait = true;
        pose0 = Random.Range(1, MaxClientDifférents);
        if(pose0 == 1) { blue.SetTrigger("blueSpawn"); }
        if(pose0 == 2) { red.SetTrigger("redSpaw"); }
        if(pose0 == 3) { doctor.SetTrigger("doctorSpawn"); }
        wait = false;
        StartCoroutine(Minuterie());
    }

    IEnumerator Trajet1(int id)
    {
        wait = true;
        pose1 = id;
        if(id == 1) { blue.SetTrigger("bluePos1"); }
        if (id == 2) { red.SetTrigger("redPos1"); }
        if (id == 3) { doctor.SetTrigger("doctorPos1"); }
        yield return new WaitForSeconds(1);
        pose0 = 0;
        wait = false;
    }

    IEnumerator Trajet2(int id)
    {
        wait = true;
        pose2 = id;
        if (id == 1) { blue.SetTrigger("bluePos2"); }
        if (id == 2) { red.SetTrigger("redPos2"); }
        if (id == 3) { doctor.SetTrigger("doctorPos2"); }
        yield return new WaitForSeconds(1);
        pose1 = 0;
        wait = false;
    }

    IEnumerator Trajet3(int id)
    {
        wait = true;
        pose3 = id;
        if (id == 1) { blue.SetTrigger("bluePos3"); }
        if (id == 2) { red.SetTrigger("redPos3"); }
        if (id == 3) { doctor.SetTrigger("doctorPos3"); }
        yield return new WaitForSeconds(1);
        pose2 = 0;
        wait = false;
    }

    IEnumerator Trajet4(int id)
    {
        wait = true;
        pose3 = id;
        if (id == 1) 
        {
            blue.SetTrigger("bluePos4");
            yield return new WaitForSeconds(1);
            GameObject clientBlue = Instantiate(ClientBlue, Wp.transform.position, Quaternion.identity);
        }
        if (id == 2) 
        {
            red.SetTrigger("redPos4");
            yield return new WaitForSeconds(1);
            GameObject clientRed = Instantiate(ClientRed, Wp.transform.position, Quaternion.identity);
        }
        if (id == 3) 
        { 
            doctor.SetTrigger("doctorPos4");
            yield return new WaitForSeconds(1);
            GameObject clientDocteur = Instantiate(ClientDocteur, Wp.transform.position, Quaternion.identity);
        }
        pose3 = 0;
        wait = false;
    }

    IEnumerator Minuterie()
    {
        yield return new WaitForSeconds(attente);
        StartCoroutine(Spawn());
    }
}
et voici une screen de la scène de unity.
Image

Si vous avez besoin de plus d’information je serais heureuse de les fournir. J'espère sincèrement que vous pourrez me donner l'astuce qu'il me manque. Bonne soirée
Dernière édition par poupoule_h5n1 le 19 Fév 2023 11:29, édité 1 fois.

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

Re: erreur étrange "not all code paths return a value"

Message par Max » 19 Fév 2023 10:13

Bonjour,

coroutine sans yield = error.
Et IEnumerator Spawn() ne semble pas en posséder, d’où l'erreur affichée.
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
poupoule_h5n1
Messages : 48
Inscription : 28 Nov 2022 20:24

Re: erreur étrange "not all code paths return a value"

Message par poupoule_h5n1 » 19 Fév 2023 11:28

c’était bien ça merci :)

Répondre

Revenir vers « Scripting »