Partage
  • Partager sur Facebook
  • Partager sur Twitter

[Unity Intermédiaire]Array out of range

Sujet résolu
    4 août 2017 à 4:25:25

    Salut tous le monde, je fais un script. En gros, il doit permettre à des IA de suivre un chemin grâce à système waypoint

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class EiMovement : MonoBehaviour {
    
    	public static Transform[] wayPoints;
    	public Transform target;
    
    
    
    	public float speed;
    	private int wayPointIndex;
    
    	// Use this for initialization
    	void Awake () {
    
    		wayPoints = new Transform[transform.childCount];
    
    		for (int i = 0; i < wayPoints.Length; i++) {
    			wayPoints[i] = transform.GetChild (i);
    		}
    
    	}
    
    	void Start(){
    		target = wayPoints[0];
    	}
    
    
    
    
    	// Update is called once per frame
    	void Update () {
    		Vector3 dir = target.position - transform.position;
    		transform.Translate (dir.normalized * speed * Time.deltaTime * speed);
    
    		if (Vector3.Distance(transform.position, target.position) <= 0.2f){
    			goNextWayPoint ();
    		}
    
    	}
    
    			void goNextWayPoint(){
    		if (wayPointIndex == wayPoints.Length -1) {
    			Destroy (gameObject);
    			return;
    		}
    			
    		wayPointIndex++;
    		target = wayPoints[wayPointIndex];
    	}
    		
    }
    
    

    J'ai une erreur, array out of range, j'ai essayé de résoudre ça mais je n'arrive pas ;(

    Si quelqu'un pouvait m'aider ?

    -
    Edité par JeanClaudeGoldenBo 4 août 2017 à 23:03:58

    • Partager sur Facebook
    • Partager sur Twitter
    Citatione
    Anonyme
      4 août 2017 à 22:16:15

      Salut,

      Je crois que le problème viens au niveau de la déclaration de ton tableau.

      • Partager sur Facebook
      • Partager sur Twitter
        4 août 2017 à 23:02:25

        Oui je sais, quand je clique sur l'erreur dans la console, sa me renvois à la ligne 27, je ne sais pas comment régler ça ;(

        -
        Edité par JeanClaudeGoldenBo 4 août 2017 à 23:06:55

        • Partager sur Facebook
        • Partager sur Twitter
        Citatione
          4 août 2017 à 23:45:22

          Je n'utilises pas Unity et je ne développe pas en C# mais je pense que tu as un problème d'initialisation.

          Afin de pouvoir t'aider, quand est appelée la fonction Awake et quand est appelée la fonction start ? Il semblerait que la fonction start soit appelée avant la fonction Awake ce qui expliquerait que tu n'aies rien à l'indice 0 du tableau.

          Une autre hypothèse serait que transform.childCount vaille 0 et donc ta boucle d'initialisation ne servirai à rien.

          • Partager sur Facebook
          • Partager sur Twitter
            5 août 2017 à 2:38:36

            De la docs d'Unity :

            "The difference between Awake and Start is that Start is only called if the script instance is enabled. This allows you to delay any initialization code, until it is really needed. Awake is always called before any Start functions. This allows you to order initialization of scripts."

            En français :

            La différence entre Awake et Start est que Start n'est appelée que si l'instance de script est activée. Cela vous permet de retarder tout code d'initialisation, jusqu'à ce qu'il soit vraiment nécessaire. Awake est toujours appelé avant toute fonction Start. Cela vous permet de commander l'initialisation des scripts.

            • Partager sur Facebook
            • Partager sur Twitter
            Citatione
              5 août 2017 à 2:51:55

              Et (je n'y connais rien alors ne me jugez pas :p ) tu ne peux pas faire ça ?

              using System.Collections;
              using System.Collections.Generic;
              using UnityEngine;
               
              public class EiMovement : MonoBehaviour {
               
                  public static Transform[] wayPoints = new Transform[transform.childCount];
                  public Transform target;
               
               
               
                  public float speed;
                  private int wayPointIndex;
               
                  // Use this for initialization
                  void Awake () {
               
                      for (int i = 0; i < wayPoints.Length; i++) {
                          wayPoints[i] = transform.GetChild (i);
                      }
               
                  }
               
                  void Start(){
                      target = wayPoints[0];
                  }
               
               
               
               
                  // Update is called once per frame
                  void Update () {
                      Vector3 dir = target.position - transform.position;
                      transform.Translate (dir.normalized * speed * Time.deltaTime * speed);
               
                      if (Vector3.Distance(transform.position, target.position) <= 0.2f){
                          goNextWayPoint ();
                      }
               
                  }
               
                          void goNextWayPoint(){
                      if (wayPointIndex == wayPoints.Length -1) {
                          Destroy (gameObject);
                          return;
                      }
                           
                      wayPointIndex++;
                      target = wayPoints[wayPointIndex];
                  }
                       
              }



              • Partager sur Facebook
              • Partager sur Twitter
                5 août 2017 à 3:08:24

                Heu :) 

                Merci comme même de l'aide mais mettre comme çà sa ne marche pas, j'ai enlever la ligne 27 et sa merche (à moitier ;( ..)

                L'<enemy> avance vers le premier waypoint, mais s'arrete et ne poursuit pas vers le 2eme waypoint.. Je vais essayé de trouver comme le faire changer de <target> à chaque fois qu'il attend une d'elle :)

                New :

                using System.Collections;
                using System.Collections.Generic;
                using UnityEngine;
                
                public class EiMovement : MonoBehaviour {
                
                	public static Transform[] wayPoints; 
                	public Transform target;
                
                
                
                	public float speed;
                	private int wayPointIndex = 0;
                
                	// Use this for initialization
                	void Awake () {
                
                		wayPoints = new Transform[transform.childCount];
                		for (int i = 0; i < wayPoints.Length; i++) {
                			wayPoints[i] = transform.GetChild (i);
                		}
                
                	}
                
                	void Start(){
                		//target = wayPoints[0];
                	}
                
                
                
                
                	// Update is called once per frame
                	void Update () {
                		Vector3 dir = target.position - transform.position;
                		transform.Translate (dir.normalized * speed * Time.deltaTime);
                
                		if (Vector3.Distance(transform.position, target.position) <= 0.2f){
                			goNextWayPoint ();
                		}
                
                	}
                
                			void goNextWayPoint(){
                		target = wayPoints[wayPointIndex];
                		wayPointIndex++;
                
                		/*if (wayPointIndex == wayPoints.Length - 1) {
                			target = wayPoints[wayPointIndex];
                			wayPointIndex++;
                			//Destroy (gameObject);
                			//return;
                		}
                			
                		wayPointIndex++;
                		target = wayPoints[wayPointIndex];*/
                	}
                		
                }
                



                -
                Edité par JeanClaudeGoldenBo 5 août 2017 à 3:36:45

                • Partager sur Facebook
                • Partager sur Twitter
                Citatione
                  5 août 2017 à 3:51:07

                  Dans ce cas, je ne peux pas trop t'aider car je travaille à fond sur PHP en ce moment et j'ai pas touché à Unity depuis un an environ.

                  Bonne chance.

                  • Partager sur Facebook
                  • Partager sur Twitter
                    6 août 2017 à 0:01:26

                    Et bien merci comme même ;)

                    -------------------------------------------

                                  2eme méssage

                    Ah j'ai trouvé une solution :)

                    J'ai mis la partie Awake() dans un autre script puis j'y accède depuis le script principal script et sa marche :)

                    Script 1 :

                    using System.Collections;
                    using System.Collections.Generic;
                    using UnityEngine;
                    
                    public class WaypointInisalize : MonoBehaviour {
                    
                    	public static Transform[] wayPoints;
                    
                    
                    	// Use this for initialization
                    	void Start () {
                    
                    
                    		wayPoints = new Transform[transform.childCount];
                    		for (int i = 0; i < wayPoints.Length; i++) {
                    			wayPoints[i] = transform.GetChild (i);
                    		}
                    	
                    	}
                    	
                    	// Update is called once per frame
                    	void Update () {
                    		
                    	}
                    }
                    


                    Script 2 : (*Script principal*)

                    using System.Collections;
                    using System.Collections.Generic;
                    using UnityEngine;
                    
                    public class EiMovement : MonoBehaviour {
                    
                    	private Transform target;
                    
                    
                    
                    	public float speed;
                    	private int wayPointIndex = 0;
                    
                    	// Use this for initialization
                    	void Awake () {
                    
                    
                    
                    	}
                    
                    	void Start(){
                    		target = WaypointInisalize.wayPoints[0];
                    	}
                    
                    
                    
                    
                    	// Update is called once per frame
                    	void Update () {
                    		Vector3 dir = target.position - transform.position;
                    		transform.Translate (dir.normalized * speed * Time.deltaTime);
                    
                    		if (Vector3.Distance(transform.position, target.position) <= 0.2f){
                    			goNextWayPoint ();
                    		}
                    
                    	}
                    
                    			void goNextWayPoint(){
                    		wayPointIndex++;
                    		target = WaypointInisalize.wayPoints[wayPointIndex];
                    
                    		/*if (wayPointIndex == wayPoints.Length - 1) {
                    			target = wayPoints[wayPointIndex];
                    			wayPointIndex++;
                    			//Destroy (gameObject);
                    			//return;
                    		}
                    			
                    		wayPointIndex++;
                    		target = wayPoints[wayPointIndex];*/
                    	}
                    		
                    }
                    

                    Merci d'avoir pris la peine de me réponde :)

                    -
                    Edité par JeanClaudeGoldenBo 6 août 2017 à 0:37:57

                    • Partager sur Facebook
                    • Partager sur Twitter
                    Citatione

                    [Unity Intermédiaire]Array out of range

                    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                    • Editeur
                    • Markdown