[SOLVED] Issue With a C# List<> !!!

hello there !

in my script I declare a c# list

using System.Collections.Generic;
public List<Cars_Place> Places5 = new List<Cars_Place>();

for those who wanna see how the Cars_Place Class looks like:

using UnityEngine;
using System.Collections;

public class Cars_Place {
    public bool Empty { get; set; }
    public Vehicule_En_Piste Vehicule { get; set; }
    public Road_Class Ma_Route { get; set; }
    public Cars_Place Previous { get; set; }
    public Cars_Place Next { get; set; }
    public int My_Number { get; set; }
    public Quaternion Quat01 { get; set; }

    public Vector3 Axes { get; set; }

    public Cars_Place( Road_Class Ma_Route1 = null, int My_Number1 = 0 )
    {
        Ma_Route = Ma_Route1;
        My_Number = My_Number1;
    }
}

during runtime all tests and all attempts to use this list proves that the list is simply null, not even empty but null;
I can’t even change the situation nor add elements;
I don’t know were the issues is so I really need help.
thanks in advance !

I’m using unity 5.2.3f1

Please read the stickies. Particularly, one at the top labeled “Using code tags properly”.

–Eric

1 Like

thanks, sincerely I was lost but now it seems obvious and even simple ! so many thanks for the help.

Your list isn’t even defined in the context of a class or struct, how are you even accessing it?

1 Like

Give it the Serializable attribute :

[System.Serializable]
public class Cars_Place {
...
}
1 Like

They’re not going to want to do that…

Car_Place has 2 fields that are also Car_Place.

The unity serializer can’t handle that at all and will throw a “serialization depth limit” error.

1 Like

Hi again,
sorry for not making things clear from the beginning… so let me precise that the list is declared in a public class extending Monobehaviour and all:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//public class Road_Class
public class Road_Class : MonoBehaviour
{
    public GameObject RUNTIME_LIST;
    [SerializeField]
    public bool On_Duty;
    [SerializeField]
    public string Road_Side;
    [SerializeField]
    public bool VolantAGauche;
    [SerializeField]
    public Vector3 Placez;
    [SerializeField]
    public float Unitzz;
    [SerializeField]
    public Active_Roads Settings;
    // ***
    [SerializeField]
    public string MyType;
    [SerializeField]
    public string WType;
    [SerializeField]
    public string EType;
    [SerializeField]
    public string NType;
    [SerializeField]
    public string SType;
    //
    [SerializeField]
    public string NWType { get; set; }
    [SerializeField]
    public string NEType { get; set; }
    [SerializeField]
    public string SWType { get; set; }
    [SerializeField]
    public string SEType { get; set; }
    [SerializeField]
    public float TurnRight;
    [SerializeField]
    public bool BTurnRight;
    [SerializeField]
    public float TurnLeft;
    [SerializeField]
    public bool BTurnLeft;
    [SerializeField]
    public float Forward;
    [SerializeField]
    public bool BForward;
    [SerializeField]
    public float TrafficLight;
    [SerializeField]
    public bool BTrafficLight;
    [SerializeField]
    public float MainAxis;
    // +++++++++++++++++++++++++++++++++++++++++++
    public FEUX01 MyLight;
    [SerializeField]
    public FEUX01 MySecondLight;
    [SerializeField]
    public Road_Dbug_List ListDbug;
    public Vector3 CP01 = Vector3.zero;
    public int CPx = 0;
    public int CPz = 0;
    //[SerializeField]
    public List<Cars_Place> Places5 = new List<Cars_Place>();
    public bool ready = false;

  
    public Quaternion Quat01;
    public float sizes;


    void Start()
    {

        ready = true;
        //AwakeMe();
    }
    public void AwakeMe()
    {
        ready = true;
        Settings = transform.parent.transform.GetComponent<Active_Roads>();
        sizes = RUNTIME_LIST.GetComponent<RunTime_Settings>().carSizes;
        float road_Length = Unitzz - ( Settings.RoadWidth * 2 );
        int NumberOfPlaces = Mathf.FloorToInt( road_Length / sizes );
        Cars_Place CP = null;
        Cars_Place New_Place = null;
        for (int i = 0; i < NumberOfPlaces; i++)
        {

            New_Place = new Cars_Place(this, i);
            New_Place.Axes = CP01;
            New_Place.Quat01 = Quat01;
            CP01 = new Vector3(CP01.x + (sizes * CPx), 0, CP01.z + (sizes * CPz));

            if (CP != null)
                CP.Next = New_Place;
            if (CP != null)
                New_Place.Previous = CP;
            CP = New_Place;
            Places5.Add(New_Place);
            print("added");
        }
    }

note that this is just an extract…
declaration of list at line 68.

the role of the public function AwakeMe() (at line 82) is to fill the list but there’s an error then because the programs is considering that it’s null instead of empty. to me it should be simply empty, not null…
there’s another script not shown here that tries to use this list with same issue…
I hope it’s clarified now.

ok I’m testing what’s Intense_Gamer94 said, I’ll get back to you quickly…

Many thanks to you because it seems okay now !
so here’s the fix:

using UnityEngine;
using System.Collections;

[System.Serializable]
public class Cars_Place {
    public bool Empty { get; set; }
    public Vehicule_En_Piste Vehicule { get; set; }
    public Road_Class Ma_Route { get; set; }
    public Cars_Place Previous { get; set; }
    public Cars_Place Next { get; set; }
    public int My_Number { get; set; }
    public Quaternion Quat01 { get; set; }

    public Vector3 Axes { get; set; }

    public Cars_Place( Road_Class Ma_Route1 = null, int My_Number1 = 0 )
    {
        Ma_Route = Ma_Route1;
        My_Number = My_Number1;
    }  
}

Thanks to you and everybody for the help !!
@lordofduct I did not experienced the error that you mentioned…

I just noticed that your references to Cars_Place are properties and not fields, so the unity serializer isn’t serializing them.

That’s why you’re not getting the error.

Just be warned, don’t include serialized fields that are the same type as the class it’s in (unless it’s a Unity object).

ok sure, thanks a lot !