Search Unity

Why this instruction at line 102 pose a problem?

Discussion in 'Scripting' started by Kaloverian, Mar 25, 2020.

  1. Kaloverian

    Kaloverian

    Joined:
    Oct 8, 2018
    Posts:
    32
    Hi,

    My code blocks at Destroy level (ObjectList [l]);
    I do not understand why?
    When this instruction is canceled, it works
    Otherwise, it blocks!

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4. using System;
    5.  
    6.  
    7.  
    8. public class multi_duplique : MonoBehaviour
    9. {
    10.     int N, i, j, q;
    11.     int k,l = 0;
    12.     public GameObject objet1, empty1;
    13.     List<GameObject> ObjectList;
    14.     List<GameObject> EmptyList;
    15.    // float ac;
    16.     // float[] acc;
    17.  
    18.     public Mesh mesh;
    19.  
    20.     //Create a custom struct and apply [Serializable] attribute to it
    21.     [Serializable]
    22.     public struct Vecteur_5
    23.     {
    24.         public float x;
    25.         public float y;
    26.         public float z;
    27.         public float vitesse;
    28.         public float accéleration;
    29.     }
    30.  
    31.     //Make the private field of our PlayerStats struct visible in the Inspector
    32.     //by applying [SerializeField] attribute to it
    33.     [SerializeField]
    34.     public Vecteur_5[] vecteur;
    35.  
    36.  
    37.     // Use this for initialization
    38.     void Start()
    39.     {
    40.         //prendre la taille du tableau vecteur à affecter à la variable N
    41.         N = vecteur.Length;
    42.         ObjectList = new List<GameObject>();
    43.         EmptyList = new List<GameObject>();
    44.         ObjectList.Add(objet1);
    45.         EmptyList.Add(empty1);
    46.         print("nombre_duplications" + N);
    47.         ObjectList[0] = objet1;
    48.  
    49.  
    50.  
    51.  
    52.  
    53.     }
    54.  
    55.     // Update is called once per frame
    56.     void Update()
    57.     {
    58.  
    59.         if (Input.GetKeyDown(KeyCode.I))
    60.         {
    61.             k++;
    62.             for (i = (int)((Mathf.Pow(N, k) - 1) / (N - 1)); i < (int)((Mathf.Pow(N, k + 1) - 1) / (N - 1)); i++)
    63.             {
    64.                 ObjectList.Add(objet1);
    65.                 EmptyList.Add(empty1);
    66.                 ObjectList[i] = new GameObject("toto" + i);
    67.                 EmptyList[i] = new GameObject("empty" + i);
    68.  
    69.                 ObjectList[i].transform.position = ObjectList[(int)Mathf.Floor((i - 1) / N)].transform.position;
    70.                 float xPos = ObjectList[(int)Mathf.Floor((i - 1) / N)].transform.position.x;
    71.                 float yPos = ObjectList[(int)Mathf.Floor((i - 1) / N)].transform.position.y;
    72.                 float zPos = ObjectList[(int)Mathf.Floor((i - 1) / N)].transform.position.z;
    73.  
    74.                 EmptyList[i].transform.position = EmptyList[(int)Mathf.Floor((i - 1) / N)].transform.position;
    75.                 float xPosEmpty = EmptyList[(int)Mathf.Floor((i - 1) / N)].transform.position.x;
    76.                 float yPosEmpty = EmptyList[(int)Mathf.Floor((i - 1) / N)].transform.position.y;
    77.                 float zPosEmpty = EmptyList[(int)Mathf.Floor((i - 1) / N)].transform.position.z;
    78.  
    79.  
    80.  
    81.  
    82.  
    83.                 Vector3 newPos = new Vector3(xPos, yPos, zPos);
    84.                 ObjectList[i].transform.position = newPos;
    85.  
    86.                 Vector3 newPosEmpty = new Vector3(xPosEmpty, yPosEmpty, zPosEmpty);
    87.                 EmptyList[i].transform.position = newPosEmpty;
    88.  
    89.                 ObjectList[i].AddComponent<MeshRenderer>();
    90.                 ObjectList[i].AddComponent<MeshFilter>().mesh = mesh;
    91.                 ObjectList[i].transform.parent = EmptyList[(int)Mathf.Floor((i - 1) / N)].transform;        //ObjetList[i] est l'enfant de l'empty
    92.                                                                                                            
    93.             }//fin du 1er for
    94.  
    95.  
    96.             //destruction des parents
    97.             for (l = (int)((Mathf.Pow(N, k - 1) - 1) / (N - 1)); l < (int)((Mathf.Pow(N, k) - 1) / (N - 1)); l++)
    98.             {
    99.                 print("valeur de k :" + k);
    100.                 print("nombre d'éléments de la liste ObjectList avant destruction :"+ObjectList.Count);
    101.              
    102.                 Destroy(ObjectList[l]); //détruire le parent :ça bloque à l'appui;n'est pas détruit
    103.                 print("valeur de l :" + l);//boucle
    104.                 print("nombre d'éléments de la liste ObjectList après destruction :"+ObjectList.Count);
    105.              
    106.  
    107.                 // break;
    108.             }//fin du 2ème for*/
    109.  
    110.  
    111.           }//fin de if
    112.  
    113.  
    114.         while (k > 0)
    115.         {
    116.             for (j = 1; j < (int)((Mathf.Pow(N, k + 1) - 1) / (N - 1)); j++)
    117.             {
    118.  
    119.  
    120.                 //ObjectList[j].transform.Translate(vecteur[(j - 1) % N].x * Time.deltaTime * (acc[(j-1)%N]+vecteur[(j-1)%N].vitesse), vecteur[(j - 1) % N].y * Time.deltaTime *(acc[(j-1)%N]+vecteur[(j-1)%N].vitesse), vecteur[(j - 1) % N].z * Time.deltaTime *(acc[(j-1)%N]+vecteur[(j-1)%N].vitesse)); //avec l'accération définie de cette manière,la direction est faussée
    121.                 ObjectList[j].transform.Translate(vecteur[(j - 1) % N].x * Time.deltaTime * (vecteur[(j - 1) % N]).vitesse, vecteur[(j - 1) % N].y * Time.deltaTime * vecteur[(j - 1) % N].vitesse, vecteur[(j - 1) % N].z * Time.deltaTime * vecteur[(j - 1) % N].vitesse); //Space.Self ne fonctionne pas
    122.                 EmptyList[j].transform.Translate(vecteur[(j - 1) % N].x * Time.deltaTime * (vecteur[(j - 1) % N]).vitesse, vecteur[(j - 1) % N].y * Time.deltaTime * vecteur[(j - 1) % N].vitesse, vecteur[(j - 1) % N].z * Time.deltaTime * vecteur[(j - 1) % N].vitesse);
    123.  
    124.  
    125.  
    126.  
    127.  
    128.  
    129.             }
    130.             break;
    131.  
    132.         }//fin de while
    133.  
    134.  
    135.         }//fin de update
    136.  
    137.     }//fin de classe
    138.  
    139.    
    140.  
    141.  
    142.  
    143.  
    144.  
    145.  
    146.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You haven't mentioned what the actual error is, but when you have an error at a line which accesses a list by index, without any code ahead of that line actually checking if that index even exists, that's a red flag.

    Add this line right before line 102
    Code (csharp):
    1. Debug.Log("ObjectList.Count: " + ObjectList.Count.ToString() + " l: " + l.ToString());
    The value of "l" must be less than the value of ObjectList.Count or you will get an index out of range error, which would be my first guess as to what error you are getting. Otherwise please state the exact error you are getting.
     
  3. Kaloverian

    Kaloverian

    Joined:
    Oct 8, 2018
    Posts:
    32
    problem solved !